How to Compare Strings in Bash

In programming languages, comparison operators are used for comparing numbers, strings, and arrays. Bash is a Unix/Linux CLI programming script used to check for conditions and perform several tasks simultaneously. We commonly use the comparison operators (=, ==, !=, >, <) to compare strings in bash.

The strings are checked character by character in Bash; if a bash string has the same length and sequence, both strings are equal. Let’s compare strings using comparison operators with this timeline:

  • Types of Comparison Operators in Bash?
  • How to Compare Strings in Bash?
    • Example 1: Check the Equality of Two Bash Strings Using (=) Operator
    • Example 2: Check the Equality of Two Bash Strings Using (==) Operator
    • Example 3: Check the Equality of Two Bash Strings Taking User Input
    • Example 4: Check the Not Equality of Two Bash Strings
    • Example 5: Check if a Word is Present in Bash String
    • Example 6: Check the Greater than Condition for Two Bash Strings
    • Example 7: Check if the Bash String is Empty
    • Example 8: Check if the Bash String is not Empty

Let’s start with comparison operators types.

Types of Comparison Operators in Bash

Bash utilizes various comparison operators to compare strings. The types of operators are used in Linux bash script as shown below:

[String1 = String2] [[String1 == String2]]Check the equality of the two strings, which returns “true” if both strings are equal.
[String != String2]It returns “true” in case both strings are not equal.
[String1 =~ CONDITION]It returns true when the CONDITION is met.
[String1 > String2]This “>” checks the lexicographical order (alphabetical order) and returns true in case String1 is greater than String2.
[String1 < String2]Returns true in case the String1 has less lexicographical order than String2.
[ -z String1]It returns true if String1 is empty.
[ -n String1]It returns true if String1 is non-empty.
[String1 && String2]It checks for AND operator and returns true if both conditions are satisfied.
[String1 || String 2]Return true in case one condition is satisfied.

Let’s compare strings in bash with the help of different comparison operators.

How to Compare Strings in Bash?

In Linux, the comparison operators compare the bash strings to provide the result. We can compare the strings based on the equality operator (=), non-equality operator (!=), greater than (>), and less than (<) operators, as discussed in this section.

Example 1: Check the Equality of Two Bash Strings Using (=) Operator

We can check the equality of the two strings using the equality operator (=), which checks character by character in both strings (should have the same length and sequence). For instance, to check the two strings (Str1 and Str2 variables) are equal or not using the if condition, use the following script:

#!/bin/bash

Str1="This is a test."
Str2="This is a test."

if [ "$Str1" = "$Str2" ]; then
    echo "Both strings are equal."
else
    echo "Both strings are different."
fi

Run the bash script using below bash command:

$ bash test_script.sh

The output shows both strings are equal.

Example 2: Check the Equality of Two Bash Strings Using (==) Operator

Similarly, we can check the equality of two strings using the “==” operator as done below:

#!/bin/bash

Str1="This is a test."
Str2="This is a test."

if [[ "$Str1" == "$Str2" ]]; then
    echo "Both strings are equal."
else
    echo "Both strings are different."
fi

Let’s run the bash script to check the output:

$ bash test_script.sh

The output compares both strings and shows that the strings are equal.

Example 3: Check the Equality of Two Bash Strings Taking User Input

We can take the user input for two or more strings and compare the strings in Linux. For example, let’s take user input for two strings and compare them using the == operator with the below bash script:

#!/bin/bash

read -p "Enter your first string: " Str1
read -p "Enter your second string: " Str2

if [[ "$Str1" == "$Str2" ]]; then
    echo "Both strings are equal."
else
    echo "Both strings are different."
fi

Use the following command to run the script:

$ bash test_script.sh

The user inputs the first string, “Hello”, and the second string, “Hi”, which is compared using the script and displays the output if both strings are different.

Example 4: Check the Not Equality of Two Bash Strings

In contrast to the equality operator, the not equality performs the tasks in case the check condition is not met. For instance, two strings are compared with the not equality operator to check if both strings are the same; utilizing the below command:

#!/bin/bash   Str1="This is a test."
Str2="This is a test."

if [ "$Str1" != "$Str2" ]; then
    echo "Both strings are different."
else
    echo "Both strings are equal."
fi

For executing the above bash script, utilized this command:

$ bash test_script.sh

The output shows that both strings have the same content.

Example 5: Check if a Word is Present in Bash String

We can compare the string to find a specific word. To find the word “sample” in a string, the below script is used:

#!/bin/bash

VAR='This is a sample text.'
if [[ $VAR =~ .*sample* ]]; then
  echo "Yes!"
fi

Execute bash script using this command:

$ bash test_script.sh

The output shows the true condition, which shows that the word “sample” is present in the string.

Example 6: Check the Greater than Condition for Two Bash Strings

Two numbers are compared based on the higher or lower number, while the strings are compared in alphabetical order. Using the bash script to check if string 1 is greater than string 2, use:

#!/bin/bash   Str1="This is a test."
Str2="This is not a test."

if [ "$Str1" > "$Str2" ]; then
    echo "String1 is greater."
else
    echo "String2 is greater."
fi

Run the below-mentioned command to execute the script:

$ bash test_script.sh

The output shows that string 1 comes after in alphabetical order rather than string 2.

Example 7: Check if the Bash String is Empty

Sometimes, in a bash script, we need to find if the string is empty; we can use the “z” and “n” options to check that. For instance, to check if the Str1 string variable is empty in the bash, the “z” option is utilized in a bash script:

#!/bin/bash

Str1="Test."

if [ -z $Str1 ]; then
    echo "String1 is empty."
else
    echo "String1 is not empty."
fi

To run the script, utilize this command:

$ bash test_script.sh

The output shows that the Str1 string variable is not empty.

Let’s implement this condition on an empty string with the below script:

#!/bin/bash

Str1=""

if [ -z $Str1 ]; then
    echo "String1 is empty."
else
    echo "String1 is not empty."
fi

Run the script using:

$ bash test_script.sh

The string is empty is shown by executing the script.

Example 8: Check if the Bash String is not Empty

We can check if the string is not empty using this script:

#!/bin/bash

Str1="Test."

if [ -n $Str1 ]; then
    echo "String1 is not empty."
else
    echo "String1 is empty."
fi

Now, execute the script with this command:

$ bash test_script.sh

The above output shows the string is not empty.

Conclusion

Several comparison operators are used to compare strings in bash, for instance, the equality operator (= or ==), the not equality operator (!=), an empty string (z), and a non-empty string (n). Moreover, we can use the greater than (>) and less than (<) operators to check the lexicographical (alphabetical) order for the strings. This post has briefly explained the various methods to compare strings in Bash.