Bash If Statements for String Comparison
Last updated
Last updated
When writing bash scripts, we frequently compare strings. This enables us to develop conditions for if-then-else blocks based on the difference or similarities between the two strings. We can also perform lexicographic comparisons and check if the input string is empty or from which character it begins.
Bash has comparison operators which compare values of two operands and return either a true or a false. We can use these comparisons with the Bash if command. If command can be employed either with the arithmetic expansion (( .. )) or with the test alias [ .. ]. We need to be careful with the former as it might produce unexpected responses which we will discuss at the end of this article. For now we will perform all the comparisons using the square brackets [].
Bash doesn’t offer a built-in tool to verify if the two strings are equal like other programming languages. However, we can achieve the same using the if statement.
Example
var1="Football"var2="Football"var3="Hockey"if
[ "$var1"
= "$var2"
]; then
echo
true; else
echo
false; fiif
[ "$var1"
= "$var3"
]; then
echo
true; else
echo
false; fiif
[ "$var1"
= "Tennis"
]; then
echo
true; else
echo
false; fiif
[ "Tennis"
= "Tennis"
]; then
echo
true; else
echo
false; fiif
[ "$var1"
== "$var2"
]; then
echo
true; else
echo
false; fiif
[ "$var1"
== "$var3"
]; then
echo
true; else
echo
false; fiif
[ $var1 == "Tennis"
]; then
echo
true; else
echo
false; fiif
[ "Tennis"
== "Tennis"
]; then
echo
true; else
echo
false; fi
We have three string variables, val1, val2 and val3. We can test the equality of two string variables, two string constants as well as a string variable and a constant using the if statement. The value of the variables can be compared by using either the single or double equal symbol (= or ==). It is a good practice to surround the variables with double quotes ("...").
Just like we are checking for equality, we can do the same for inequality by doing the negation using the Not (!) operator.
Example
var1="Football"var2="Football"var3="Hockey"if
[ "$var1"
!= "$var2"
]; then
echo
true; else
echo
false; fiif
[ "$var1"
!= "$var3"
]; then
echo
true; else
echo
false; fiif
[ "$var1"
!= "Tennis"
]; then
echo
true; else
echo
false; fiif
[ "Tennis"
!= "Tennis"
]; then
echo
true; else
echo
false; fi
We again have three string variables, val1, val2 and val3. We can test the inequality of two string variables, two string constants as well as a string variable and a constant using the if statement. The value of the variables can be compared by using either the not equal symbol (!=).
We can check if the string contains a substring using regular expressions.
Example
var1="Football"var2="Foot"if
[[ "$var1"
= *"$var2"* ]]; then
echo
true; else
echo
false; fiif
[[ "$var1"
=~ .*"$var2".* ]]; then
echo
true; else
echo
false; fi
We are using two methods to check if the given string contains a substring. We need to cover the substring with asterisk symbols (*). The asterisk symbol tells Bash to match all the characters before and after the letters "Foot". The other approach uses the regex operator =~ and the dot (.) with an asterisk. The regex matches zero or more occurrences of any character. The [[ is taken to execute the conditional command.
Sometimes we want to read line-by-line and check if a string stored in a variable begins with some character. To achieve this, we simply need to follow the same approach of using the regex which we saw above.
Example
var1="Football"if
[[ "$var1"
= F* ]]; then
echo
true; else
echo
false; fiif
[[ "$var1"
= H* ]]; then
echo
true; else
echo
false; fi
We have a regex * to check if the string variable starts with "F" followed by any number of characters.:
Lexicography is the activity of writing and editing dictionaries. In lexicographical comparison, two strings are compared alphabetically. This involves comparing the characters in a string sequentially from left to right.
Example
var1="Football"var2="Hockey"if
[[ "$var1"
> "$var2"
]]then echo
$var1 is greaterelif
[[ "$var1"
< "$var2"
]]then echo
$var2 is greater else echo
$var1 and $var2 are equalfi
Lexicographical comparison involves comparing the characters in a string sequentially from left to right. The first letter of var1, F gets compared with var2, H. As H comes later than F, the second if condition will be executed.
We often need to develop conditions to check if the string is empty or not. To achieve this, we can use the -n or -z option available with if command.
Example
var1="Football"var2=""if
[ -z "$var1"
]; then
echo
true; else
echo
false; fiif
[ -z "$var2"
]; then
echo
true; else
echo
false; fiif
[ -n "$var2"
]; then
echo
false; else
echo
true; fiif
[ ${#var2} -eq 0 ]; then echo true; else echo false; fi
The -n and -z option available with the if statement determines whether the string is empty or not. The alternate solution is to find the length of the variable holding the string. If it is zero, it's safe to say that the string is empty.
Let's see what happens if we use the arithmetic expansion for string comparisons:
Example
str1=astr2=bif
(( $str1 == $str2 )); then
echo
true; else
echo
false; fi
Why are we getting the wrong result?
We are aware that (()) is an arithmetic construct and a string is taken as the name of a variable and the value of that variable is used. This happens after variables expand. Hence, Bash looks at the variables "a" and "b". Since both the variables are unset, both are considered zero. That is why they are equal and we get the result as true.
We often compare strings while writing Bash scripts.
We can apply the if commands on strings to compare them.
Regular expressions with string variables and constants inside the if condition help determine if the substring of a string exists.
We should not use (()) with if for string comparisons.