Comparing strings is a common task when working with shell scripts, user input, or string data. In the shell, you may need to check if a value exists in another string, find if two strings have the same length, test for the beginning or end of a word, or any other type of comparison. The standard Unix tools don’t provide many options for comparing strings; the traditional lexical comparison (like checking if two words are the same length and comparing each character individually) is hard to implement efficiently in code and isn’t as valuable for a dynamic language like Bash.
This article explains different ways to compare strings in Bash and several related commands.
String Comparison Operators
We can use comparison operators with bash if statements to compare two strings. Here is the list of comparison operators to work with strings in the bash shell.
Operator | Details |
---|---|
string1 == string2 | Returns true if both strings are equal. |
string1 != string | Returns true if both strings are not equal. |
string =~ regex | Compare string1 with regular expression and return true matches |
-z string | Return true if the given string length is zero (0) |
-n string | Return true if the given string length is not zero |
Now, we will discuss the above comparison operator one by one with examples.
Compare Two Strings in Bash (==)
If you need to check if two strings are equal, use the ==
operator. These operators compare the left operand with the right operand and return true if both match.
Let’s understand with an example. In a shell script initialize two variables with a string. Then use the if statement to compare whether both strings are equal or not using the == operator.
1 2 3 4 5 6 7 8 9 10 11 | #!/usr/bin/env bash STR1="TecAdmin" STR2="TecAdmin" if [ "$STR1" == "$STR2" ] then echo "Both strings are equal" else echo "Both strings are not equal" fi |
Run the above shell script in a bash shell and check for the results.
Output:
Both strings are equal
Now, change both variables’ values with different strings. Then again the script and see the results.
Check Two Strings are Not Equal (!=)
Sometimes we are required to check if both strings are not equal. You can use !=
operator to test if two strings are not equal. Let’s check with an example:
1 2 3 4 5 6 7 8 9 10 11 12 | #!/usr/bin/env bash STR1="TecAdmin" STR2="HelloWorld" ##Check if both strings are not equal if [ "$STR1" != "$STR2" ] then echo "True, both strings are not equal" else echo "False, both strings are equal" fi |
Run the above shell script in a bash shell and check for the results.
Output:
True, both strings are not equal
Compare Strings with Regular Expression
We can also compare string with a regular expression in bash scripts. While using the string comparison with regular expression with an if statement, you should always enclose with [[ ]]
quotes. The below example will show help you to check if the variable contains the string that begins with a specific string.
1 2 3 4 5 6 7 8 9 10 | #!/usr/bin/env bash STR="TecAdmin" if [[ "$STR" =~ ^Tec ]] then echo "Yes, the regular expression matches " else echo "Regular expression not matches " fi |
Output:
Yes, the regular expression matches
Let’s check with another example. In this script, we will prompt the user to input a number. Then verify whether the input value is a number or not. A number container the digits between 0 to 9.
1 2 3 4 5 6 7 8 9 10 11 | #!/usr/bin/env bash read -p "Input a number: " VAR ## Check if the input value is a number if [[ "$VAR" =~ ^[0-9]+$ ]] then echo "Given input is a number" else echo "Sorry, input is not a number" fi |
Run the above bash script and provide the input when prompted.
First run:
Input a number: 12
Given input is a number
Again run this script but this time input a non-numeric value and see the results.
Second run:
Input a number: 1234a
Sorry, input is not a number
Check if a String is Empty
While taking user input in a shell script, it’s important to check that the input string is not empty. You can use -z
returns true if the string is empty.
1 2 3 4 5 6 7 8 9 | #!/usr/bin/env bash read -p "Type anything: " VAR if [[ -z $VAR ]]; then echo "Empty string" else echo "You type: ${VAR}" fi |
Execute the above shell script in a bash shell and just hit enter when prompted for user input.
First run:
Type anything:
Empty string
Again run the above script and type something when prompted.
Second run:
Type anything: TecAdmin
You type: TecAdmin
Conclusion
In this tutorial, we have discussed string comparisons in the bash script. You can also check if a string is empty or not. Also provides an example to check if the input value is a number or not.