Comparing Strings in a Bash Script

Dealing with strings is normal in any programming language because of the need to compare the values of these strings for various types of programming purposes. Comparing strings is defined as the process of examining and outputting the differences or similarities of in characters among two strings. In this tutorial, you will learn how to compare the string values in Bash.

Operators Used When Comparing Strings

In testing the equality and inequality of two strings, the if statement is used. If you want to check if the two strings are equal, you may use the comparison operator “==”, and if you want to check the inequality of the strings, you may use the operator “!=”.

The table below shows the list operators used when comparing string values:

Operator

Definition

= with test [ command

 

Equality

 

The strings are equal.

== with [[ for pattern matching

!=

Inequality

The strings are not equal.

=~

Regex

The left string is the same the extended regular session on the right.

Greater than

The left string is greater than the right in alphabetical order.

Less than

The right string is greater than the left in alphabetical order.

-z

 

The length of the string is null or empty.

-n

 

The length of the string is non-zero.

Important Things to Remember When Comparing Strings

  • It is recommended to use a blank space between the operands and the binary operator.
  • Splitting of words can be avoided by always putting double quotes around the string variable names.
  • Depending on the situation, variables are usually treated as integer or string but they are not separated by “type”.

Examples

1.The Strings are Equal

Similar to other programming languages, bash does not have an integrated function for checking the equality of two string values.  Two string variables, strval1 and strval2 are used in the following script. To test if they are equal, the script uses the if statement and operator “=”.

Syntax: STRING 1 == STRING 2

Source: https://linuxhint.com/compare_strings_bash/
Output: Strings are equal in the first comparison and not equal in the comparison)

2.The Strings are Not Equal

The example below shows that the two string variables are not equal. The comparison operator “!=” and if statement are used.  “Windows operating system” will print if the condition is correct. Otherwise, “Linux Operating System” will print.

Syntax: STRING1 != STRING2

Output:

Source: https://linuxhint.com/compare_strings_bash/
Output: Strings are not equal

3. The String Contains a Substring

Synatx: *STRING* 

To determine if a string has a substring, use an asterisk “*”and surround the substring with it.  The purpose of the symbol is to match all the characters. 

If you take a look at the example script below, the word “internet” can be found in the string variable strval=”Microsoft Internet Explorer”. This will give you a result of partially match in the firs if. However, by using the word “internet“only, you will get false as a return for the second if. 

Output:

https://riptutorial.com/bash/example/2475/string-comparison-and-matching provides more examples of string comparisons and matching in bash.

4. The String is Empty

There are instances when you have to check if a string variable is empty or not. In this case, operators -z and -n are used. In the script below, operator -z is used and the result is true (string is empty).

Syntax: -z STRING (Check if the string length is zero.)
                 -n STRING (Check if the string length is non-zero.)

5. Lexicographic Comparison

The comparison operator > is used to check if the left string is greater than the right in alphabetical order, while < is used to check if the right string is greater than the left string in alphabetical order. This operation is done by comparing the characters in a string consecutively starting from left to right. 

Lexicographic comparison is used to compare the strings in the scripts below:

The script will produce the following result:

Conclusion

This completes our tutorial on how we can use string comparison in bash scripts. Even though comparing string is one of the simplest operations in scripting and you think you have read and understood the examples, the best way to learn is to practice using the operators yourself. 

We will be happy to hear your thoughts

Leave a reply

Coders Eye - Web Dev Tutorials and How-To Guides for Beginners
Logo
Enable registration in settings - general