How To Compare Strings In Bash
This tutorial describes how to compare strings in Bash.
When writing Bash scripts you will often need to compare two strings to check if they are equal or not. Two strings are equal when they have the same length and contain the same sequence of characters.
Comparison operators are operators that compare values and return true or false. When comparing strings in Bash you can use the following operators:
string1 = string2 and string1 == string2 – The equality operator returns true if the operands are equal.
Use the = operator with the test [ command.
Use the == operator with the [[ command for pattern matching.
string1 != string2 – The inequality operator returns true if the operands are not equal.
string1 =~ regex– The regex operator returns true if the left operand matches the extended regular expression on the right.
string1 > string2 – The greater than operator returns true if the left operand is greater than the right sorted by lexicographical (alphabetical) order.
string1 < string2 – The less than operator returns true if the right operand is greater than the right sorted by lexicographical (alphabetical) order.
-z string – True if the string length is zero.
-n string – True if the string length is non-zero.
Following are a few points to be noted when comparing strings:
A blank space must be used between the binary operator and the operands.
Always use double quotes around the variable names to avoid any word splitting or globbing issues.
Bash does not segregate variables by “type”, variables are treated as integer or string depending on the context.
Check if Two Strings are Equal
In most cases, when comparing strings you would want to check whether the strings are equal or not.
The following script uses the if statement and the test [ command to check if the strings are equal or not with the = operator:
When the script is executed it will print the following output.
Here is another script that takes the input from the user and compares the given strings. In this example, we will use the [[ command and == operator.
Run the script and enter the strings when prompted:
You can also use the logical and && and or || to compare strings:
Check if a String Contains a Substring
There are multiple ways to check if a string contains a substring.
One approach is to use surround the substring with asterisk symbols * which means match all characters.
The script will echo the following:
Another option is to use the regex operator =~ as shown below:
The period followed by an asterisk .* matches zero or more occurrences any character except a newline character.
Check if a String is Empty
Quite often you will also need to check whether a variable is an empty string or not. You can do this by using the -n and -z operators.
Comparing Strings with the Case Operator
Instead of using the test operators you can also use the case statement to compare strings:
Lexicographical comparison is an operation where two strings are compared alphabetically by comparing the characters in a string sequentially from left to right. This kind of comparison is rarely used.
The following scripts compare two strings lexicographically:
The script will output the following:
Comparing string is one of the most basic and frequently used operations in Bash scripting. After reading this tutorial, you should have a good understanding of how to compare strings in Bash. You can also check our guide about string concatenation.