Do I Compare Two String Variables in an ‘if’ Statement in Bash

Bash is the most frequently used programming environment for generating scripts. Comparison of two strings in bash is very common to check the equality/inequality of the strings. The user can utilize the “==” and “!=” to compare strings.

This post will cover examples of comparing the two strings variable using the “if” statement in the bash script.

  • Compare Two Strings Variable in Bash Script
  • Compare Through “==” Operator
  • Compare Through “!=” Operator
  • Comparison Of User Input

Compare Two Strings Variable in Bash Script

For the comparison of two string variables in the bash script, the following “if” syntax is utilized.

Syntax

if [ <string1> <Operator> <string2> ]
then
echo statement
fi

The syntax is defined as 

  • Enter the variables for “string1” and “string2” and desired <operator> (==, !=) in between them.
  • The “then” section prints the echo statement if the condition is fulfilled.

Example 1: Compare Through “==” Operator

The following script checks the equality of two strings:

#!/bin/bash

#String Variables
string1="String to Compare"
string2="String to Compare"

#Check Strings
if [ "$string1" == "$string2" ]
then
echo "Strings Are Equal."
fi

The script is defined as 

  • Two strings are initialized with the “string1” and “string2” variables.
  • The “if” condition to whether both strings are equal.
  • The echo command prints the appropriate message if the strings are equal.

Save the above script and exit.

Run the above script through the bash command:

$ bash script.sh

Both strings (string1 and String2) are equal 

Example 2: Compare Through “!=” Operator

The given script checks the inequality of the two strings using the “!=” operator:

#!/bin/bash

#String Variables
string1="String"
string2="String to Compare"

#Check Strings
if [ "$string1" != "$string2" ]
then
echo "Strings Are Not Equal."
fi

The script is defined as 

  • Two strings are initialized with the “string1” and “string2” variables.
  • The “if” condition checks the inequality of the strings.
  • The echo command prints the appropriate message if the strings are not equal.

Save the given script and exit.

Execute the script file to generate results:

$ bash script.sh

The given strings (string1 and string2) are not equal.

Example 3: Comparison Of User Input

To compare the strings from the user input, the following script is considered:

#!/bin/bash

#Take Input From the User
read -p "Enter First String: " string1
read -p "Enter Second String: " string2

#Check the Entered Strings
if [ "$string1" == "$string2" ]
then
echo "Strings Are Equal."
elif [ "$string1" != "$string2" ]
then
echo "Strings Are Not Equal."

fi

The script is defined as

  • The “read” property takes the input from the user in the “string1” and “string2” variables.
  • The “if” condition checks the quality of the strings and prints an appropriate message if the condition is satisfied.
  • The “elif” condition checks the inequality of the strings only if the above condition fails and prints an appropriate message if the condition is satisfied.

Save the script and exit from the editor. 

Execute the script file using the bash command to obtain results:

$ bash script.sh

The user entered strings “Henry” and “Foss” are not equal while strings “Henry” and “Henry” are equal can be seen in the above image.

Conclusion

The string variables are compared in an“if” statement to check the equality/inequality of the strings using the “==” or “!=” operator. Define the strings in the bash script or take the input from the user and generate results accordingly. 

This write-up has illustrated the various examples to compare the two string variables using the “if” statement in the bash script.