In any programming language, the task of comparing strings is generally required. For example, you need to take the user input for the password, phone number, email etc. and then need to compare it with the value saved in the database.
Similarly, you may be using the case statement and upon entering a value, you need to match a pattern and perform the action based on matched string etc.
Bash also provides ways of comparing string and this is the topic of this tutorial. In the following section, I will show you ways and examples of how to compare strings in Bash Shell scripting.
The examples include:
- Comparing the equality of two strings by “==” operator
- Check if two strings are not equal by “!=” operator
- Testing if a given string is empty by “-z” operator
- Check if the string is non-empty by “-n” operator
See the examples with the code below.
An example of comparing strings in Bash by == operator
In this example, we will check the equality of two strings by using the “==” operator. For that, we have two string variables and check the values in the if condition.
If both strings are equal, the equality message is displayed:
str1="bash" str2="bash" if [[ "$str1" == "$str2" ]]; then echo "Both strings are the same!" else echo "Strings are different!" fi
Both strings are the same!
What if the case of a string was different?
Using the same word in both strings as in the above example, except for I changed the letter “b” to “B” for the first string. See the code and output:
#Only case is different for a letter str1="Bash" str2="bash" if [[ "$str1" == "$str2" ]]; then echo "Both strings are the same!" else echo "Strings are different!" fi
Strings are different!
So, as using the “==” operator, keep in mind that the strings are compared with case-sensitivity.
So, how to perform case-insensitive comparison?
As you may not control user inputs and you want to perform case insensitive comparison, you may use the nocasematch feature provided by Bash.
I am using the same example as in the above case and just added the following line:
shopt -s nocasematch
The example script:
shopt -s nocasematch str1="Bash" str2="bash" if [[ "$str1" == "$str2" ]]; then echo "Both strings are the same!" else echo "Strings are different!" fi
and the output now is:
Both strings are the same!
Using the != operator for in-equality
You may also use “Not equal to” operator i.e. “!=” to compare strings. The example below shows using the “!=” operator:
#Not equal to demo string1="This is Bash String Comparison Tutorial" string2="String Comparison Tutorial" if [[ "$string1" != "$string2" ]]; then echo "Different!" else echo "Same!" fi
The example of checking a string is empty
In certain situations, you may require checking a string variable is empty or not before processing further or taking some action based on that variable.
By using “-z” operator, you may check if a string is empty or not. In the example below, I declared a variable with empty value and then checked its value in the if condition with -z operator:
strName = '' if [[ -z $strName ]]; then echo "Name cannot be empty!" fi
Name cannot be empty!
As string variable is empty, so –z returned True and the statement is executed in the if condition.
How to use –n operator
Similarly, the “-n” operator checks if a string is non-empty. It returns true if a given string is not empty. The example below shows its usage:
strTest='hello' if [[ -n $strTest ]]; then echo "The string contains some text." fi
The variable contains some text.