Just like other variables, you can also compare strings in Bash. Bash allows you to compare strings with the help of different string comparison operators. In this guide, we will use a few different techniques to compare strings in the Bash programming language.
String Comparison Examples in Bash
To compare two strings in Bash, you need to consider the following examples:
Example # 1: Using the Equality Operator
In this example, we will make use of the “equality” operator in Bash to compare two strings as shown in the script below.
In this script, we have created two strings “str1” and “str2” and have assigned to them two different values. After that, we have printed these strings on the terminal. Then, we have used the equality operator within an “if” statement to compare the values of these two strings. Upon a successful match, this statement will print the message saying, “The two strings are equal”, otherwise, it will print “The two strings are not equal”.
To run this script, we will execute the following command in the terminal:
$ bash comparison.bash
From the output of this script shown in the image below, you can see that the two given strings are not equal.
Example # 2: Using the Inequality Operator
Now, we will use the “inequality” operator in Bash to compare two strings as shown in the following script:
In this script, we have initialized two strings “str1” and “str2” with same values. Then, after printing their values on the terminal, we have used the inequality operator in Bash within an “if” statement. This operator will check if these two strings are unequal or not and will then print the results accordingly.
The output of this script shown in the image below implies that the two strings are equal.
Example # 3: Partial String Comparison
In this example, we will try to perform partial string comparison i.e. we will check if a given fragment is part of the original string or not. For that, we have written the following script:
In this script, we have initialized a string “samplestring” with a complete sentence. Then, we have used the equality operator in Bash to check if a given fragment is a part of the initialized string or not.
The output of this script shown in the image below confirms that the given fragment exists within our declared string.
Example # 4: String Comparison with User Input
Finally, we will perform a string comparison with the user-provided input as depicted in the following script:
In this script, we have taken the user name as input upon runtime. Then, we have compared this input with the name “John”. Upon a successful match, a message saying “This user exists within our database” will appear on the terminal, otherwise, the message “This user does not exist within our database” will appear on the terminal.
Upon execution, you will be asked to enter your name as shown in the image below:
We have entered the name “Clara” as shown in the following image:
Since the provided input is a mismatch, therefore, the output shown in the image below appeared on the terminal:
Now, we will execute this script again and will enter the name “John” as shown in the following image:
This time, a success message will appear on the terminal as shown in the image below:
In this article, we wanted to share with you the methods of comparing strings in Bash. In this regard, we shared four different examples with you that elaborated on different techniques of comparing strings in Bash. After going through these examples, you will have a pretty good idea of string comparison in Bash.