The Bash scripts also work with the string, so it is possible to compare the string in bash. We will see the comparison of the strings in Bash scripting, especially with the Bash scripts if/else method. The string is a combination of characters. The strings are identical if they have the same characters in the same order. We need to check whether the strings are equal or needed for this, we have to compare the bash strings. The strings are compared with the comparison operator and bash supports almost all the comparison operators.
Example 1: Bash String Comparison with “=” Operator in Ubuntu 22.04
We have an equality “=” operator in bash for comparison of the string. The “=” operator determines the given bash strings are equal. We will use the “=” operator in the if-else statement for the string comparison in bash.
We have created the bash file where we have implemented the bash program to compare two strings. First, we have defined the attributes “str1” and “str2” which are initialized with the different strings. The strings will be printed as we have used with the echo commands. Then, we utilized the if-else condition for string comparison. The if statement is employed with the “=” operator. The “=” operator is used between the “str1” and “str2” to determine if the specified strings are equal or not. After the comparison of the bash string, the echo statements from the if or else will be executed.
We have printed the specified string and they are not equal. So, the string comparison method returned the else statement “The strings are not equal”.
Example 2: Bash String Comparison with “==” Operator in Ubuntu 22.04
As the bash script does not have any built-in function for string comparison like other programming languages, we used these operators for the bash string comparison. In the above example, we used the equality operator. But we can also compare the string with the double equality “==” operator.
Here, we have used the read command with the “-p” option for strings input by the user. The user enters the name and the degree name as the string. The strings will be stored by the attributes “string1” and “string2”. After that, we have an if-else statement where we have compared the strings “string1” and “string2” by using the “==” operator. If the two strings are compared, it will determine whether they are identical. The echo statements of the if-else will be returned upon string comparison.
The user inputs the strings on the shell script. Then, these strings are compared with the equality “==” operator and they are not the same. Hence, the string equality results are displayed on the screen.
Example 3: Bash String Comparison with “!=” operator in Ubuntu 22.04
We have checked the equality to compare the bash strings. Now, instead of checking the equality, we will check the inequality between the strings. For this, we have an operator “!=” which is called the “not equal to” operator.
We have given a string value inside the attribute “MyString”. Then, we have called this attribute in the if statement. The if statement holds the “MyString” and the other string value for inequality comparison with the “!=” operator. The echo statement will be displayed if the strings are not equal. The echo statement of else will execute when the strings are matched.
The bash strings comparison displayed the inequality statement as the bash strings have different values.
Example 4: Bash String Comparison with “=”, “<”, and “>” operators in Ubuntu 22.04
Now, we have used other operators like greater than and less than for comparison of the bash strings. We have utilized all these operators with the elif conditions of the bash script.
We have created two bash attributes “MyStr1” and “MyStr2” which are assigned with the string values to these attributes. Then, we constructed the elif conditions where these strings are compared. Between these strings, we have used the equal “=” operator, less than the “<” operator, and the greater than “>” operator. One statement will be executed upon a comparison of the bash string attributes.
As the strings are not equal so the first statement is false, we have a second statement for greater than which is also false. The last statement of less than comparison of the string is printed as it returns true results.
Example 5: Bash Empty String Validation with “z” Operator in Ubuntu 22.04
Bash allows us to determine whether a string is empty or null by using the “-z” operator. The “-z” operator determines whether the string length is zero or not. The condition is met if the string attribute has no value.
We have declared the bash attribute as “StrVal” which has the empty ‘’ string. After this, we used the “-z” operator with the “StrVal” attribute. The “-z” operator verifies the bash string is empty or has some string value.
As the string is empty, the “-z” operator returns true. The if statement is printed on the bash shell.
Example 6: Bash Non-Empty String Validation with “n” Operator in Ubuntu 22.04
The “-n” operator is used to validate that the bash string is not empty. The condition will allow us to determine if the provided bash string is not empty. Its functionality is demonstrated by the example below:
The string attribute is declared as “StringIs” and is initialized with the non-empty string value. Then, we have an if condition that uses the bash “-n” operator for validating the non-empty string. We have also specified the attribute “StringIs” inside the if condition with the “-n” operator.
The string is not empty as verified by the “-n” operator so the statement of string existence is displayed below.
Example 7: Bash String Comparison for a Substring in Ubuntu 22.04
We will compare the string with the substring by surrounding the substring with the asterisk “*” symbol which matches all the characters of the string.
We have defined the bash string to the bash attribute. Then, we compared the substring “bash” with the string in “Attribute”. If the given string contains the substring, then, the echo message will be obtained.
The “bash” substring is found in the given bash string, so the substring exists message is seen on the bash terminal.
Conclusion:
String comparison in bash will be simpler after accomplishing the aforementioned examples. We have used the equality operator for the bash string comparison which takes the strings from the users in the bash terminal. Then, we utilize the inequality operator for the comparison of the strings. We have also compared the bash strings with the “/<” and “/>” operators. Furthermore, bash provides the “-z” and “-n” operators for validating the empty and non-empty strings which are explained in the bash script.