How to check if string contains numbers, letters, characters in bash


Written by - Deepak Prasad

bash check if string contains numbers. bash check string contains letters. bash check if string contains special characters. bash check if string starts with character. bash compare strings. shell script test variable is numeric. Check string only contains numbers. shell script to check whether a character is vowel or consonant. shell script to check if string contains vowels. bash check if string starts with character. bash check if string ends with character. shell script to check whether a character is alphabet digit or special character. bash check if string contains vowels. bash check if string starts with character. bash shell script to check if string contains numbers. shell script to check if string contains only numbers. shell script to check if string contains special characters.

 

Compare Strings in Bash

In my last article I shared some examples to get script execution time from within the script. I will continue with articles on shell scripts. In this article i will share examples to compare strings in bash and to check if string contains only numbers or alphabets and numbers etc in shell script in Linux. Now there can be various scenarios here, I will try to cover as many as possible.

 

1. Check if string contains numbers

Suppose you wish to compare strings and check if string contains numbers. Now this variable can also have characters, alphabets but most importantly we wish to check if string contains numbers. Use the below syntax in your script to compare strings and check if string contains numbers.

$VAR =~ [0-9]

I will frame this in a script

#!/bin/bash

while true; do
   read -r -p "Enter a string: " VAR
   if [[ $VAR =~ [0-9] ]];then
      echo "Input contains number"
   else
      echo "Input contains non numerical value"
   fi
done

Now let us execute our script, as you observe the script is able to differentiate between numerical and non numerical value

# /tmp/script.sh
Enter a string: test
Input contains non numerical value
Enter a string: test1234
Input contains number
Enter a string: test@123
Input contains number
Enter a string: testing
Input contains non numerical value

 

2. Check if string contains only numbers

Suppose you wish to compare strings and check if string contains only numbers. Now above we had a case where a string could contain both letter and alphabets. Try using the below syntax in your script to compare strings and check if string contains only numbers

$VAR =~ ^[0-9]+$

OR

$VAR =~ ^[[:digit:]]+$

I will frame this in a script to test multiple values

#!/bin/bash

while true; do
   read -r -p "Enter a string: " VAR
   if [[ $VAR =~ ^[0-9]+$ ]];then
      echo "Input contains number"
   else
      echo "Input contains non numerical value"
   fi
done

Now let us execute our script to compare strings, as you observe the script is able to differentiate between numerical and non numerical value so we are successfully able to check if string contains only numbers.

# /tmp/script.sh
Enter a string: test
Input contains non numerical value
Enter a string: 1234
Input contains number
Enter a string: test1234
Input contains non numerical value
Enter a string: 012
Input contains number

 

3. Check if string contains only alphabets and numbers

To compare strings and check if a string contains only alphabets and numbers you can use either of the below syntax.

$VAR =~ ^[[:alnum:]]+$

OR

$VAR =~ ^[0-9a-zA-Z]+$

Let me frame it in a script, i will demo only one of the syntax but both should work for you

#!/bin/bash

while true; do
   read -r -p "Enter a string: " VAR
   if [[ $VAR =~ ^[[:alnum:]]+$ ]];then
      echo "OK: Contains alphabets and numbers"
   else
      echo "NOK: Contains special character"
   fi
done

Execute the script. As you see we are able to differentiate the strings with alphabets and numbers and without alphabets and numbers

# /tmp/script.sh
Enter a string: 123
OK: Contains alphabets and numbers
Enter a string: abc123
OK: Contains alphabets and numbers
Enter a string: abc@123
NOK: Contains special character
Enter a string: abc.123
NOK: Contains special character

 

4. Check if string starts with character

Here let me show you an example to compare strings to check if string starts with specific character or word. Now assuming I wish to make sure that a variable or string starts with character "A". Try the below syntax to check if string starts with character "A"

$VAR =~ ^[A]

I will frame this in a script to test multiple values

#!/bin/bash

while true; do
   read -r -p "Enter a string: " VAR
   if [[ $VAR =~ ^[A] ]];then
      echo "Input starts with A"
   else
      echo "Input does not starts with A"
   fi
done

Now let us execute our script, as you observe the script is able to check if string starts with character "A".

# /tmp/script.sh
Enter a string: Test
Input does not starts with A
Enter a string: Apple
Input starts with A
Enter a string: apple
Input does not starts with A
Enter a string: Ball
Input does not starts with A

Similarly you can modify the logic to check for any other alphabet or character. For example to check for both small and capital alphabet use the below syntax

$VAR =~ ^[aA]

 

5. Check if string contains special characters

To compare strings and check if string contains special characters there are two methods. Either we match the pattern with all the available special characters or we do the opposite. We have a logic to check if a string contains only alphabets and numbers, we use that pattern and reverse it to get to know & check if string contains special characters.

$VAR == *['!'@#\$%^\&*()_+]*

OR

! $VAR =~ ^[[:alnum:]]+$

Let us frame this pattern in a script

#!/bin/bash

while true; do
   read -r -p "Enter a string: " VAR
   if [[ ! $VAR =~ ^[[:alnum:]]+$ ]];then
      echo "contains special character"
   else
      echo "does not contains special character"
   fi
done

Now if we execute the script to check if string contains special characters, we observe that the script is able to tell us about the string with and without special characters.

# /tmp/script.sh
Enter a string: test123
does not contains special character
Enter a string: 12345
does not contains special character
Enter a string: test
does not contains special character
Enter a string: test$123
contains special character
Enter a string: test%123
contains special character
Enter a string: test.123
contains special character

 

6. Check if string contains vowels

Now to compare strings and check if string contains vowels we can use below pattern. Here we are defining all the vowels in the pattern to check if string contains vowels.

$VAR =~ [AEIOUaeiou]

Let us put this in our script

#!/bin/bash

while true; do
   read -r -p "Enter a string: " VAR
   if [[ $VAR =~ [AEIOUaeiou] ]];then
      echo "OK: Contains vowels"
   else
      echo "NOK: No vowels found"
   fi
done

Execute the script to verify the functionality and if our script is able to check if string contains vowels. As you observe now our script is able to identify and check if string contains vowels or it only contains consonant.

# /tmp/script.sh
Enter a string: Apple
OK: Contains vowels
Enter a string: Bat
OK: Contains vowels
Enter a string: Test
OK: Contains vowels
Enter a string: Myth
NOK: No vowels found
Enter a string: Witch
OK: Contains vowels
Enter a string: Cry
NOK: No vowels found
Enter a string: Sky
NOK: No vowels found

 

Lastly I hope the steps from the article to compare strings and check if string contains numbers, letters or special characters in shell script in Linux was helpful. So, let me know your suggestions and feedback using the comment section.

Views: 18

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can reach out to him on his LinkedIn profile or join on Facebook page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

2 thoughts on “How to check if string contains numbers, letters, characters in bash”

Leave a Comment