At the time of writing bash scripts, it’s often required to compare two strings to check it wether those are equal or not. Two strings are equal when they have the same length and contain the same sequence of characters. This guide shows you how to compare strings in Bash.
Comparison operators are comparing the values and returns true or false. When comparing strings in Bash you can use the following operators:
- You can check like
string1 = string2and
string1 == string2and it will returns true if the operands are equal.
- Use the
=operator with the test [ command.
- Use the
==operator with the [[ command for pattern matching.
- Use the
string1 != string2– It returns true if the operands are not equal.
string1 =~ regex– Returns true if the left operand matches the extended regular expression on the right.
string1 > string2– It will return true if the left operand is greater than the right sorted in alphabetical order.
string1 < string2– The less than operator returns true if the right operand is greater than the right sorted by alphabetical order.
-zstring – True if the string length is zero.
-nstring – Returns True if the string length is non-zero.
Following points you should keep in mind while comparing strings:
- There must be a blank space between the binary operator and the operands.
- Use the double quotes around the variable names to avoid any word splitting issues.
- Variables in bash are treated as integer or string depending on the context, Bash does not separate variables by “type”.
Check if Two Strings are Equal
Generally, we need to check that string are equal or not while comparing the strings. Below is an example of script to use the if statement and the test
[ command to check if the strings are equal or not with the
#!/bin/bash VAR1="TecNStuff" VAR2="TecNStuff" if [ "$VAR1" = "$VAR2" ]; then echo "Strings are equal." else echo "Strings are not equal." fi
The script will show the following output.
Strings are equal.
In below example, it will take input from the usr and compares the given strings. In this used the
[[ command and
#!/bin/bash read -p "Enter first string: " VAR1 read -p "Enter second string: " VAR2 if [[ "$VAR1" == "$VAR2" ]]; then echo "Strings are equal." else echo "Strings are not equal." fi
Now run the script and you will be asked to enter the strings:
Enter first string: TecNStuff Enter second string: Debian Strings are not equal.
It also allows you to use the logical and
&& and or
|| to compare strings:
[[ "string1" == "string2" ]] && echo "Equal" || echo "Not equal"
Check if a String Contains a Substring
You can check that if a string contains a substring by multiple ways. It’s a simple method using asterisk
* symbols to match all characters.
#!/bin/bash VAR='Lion is a King of forest.' if [[ $VAR == *"Lion"* ]]; then echo "It's contains." fi
The script will echo the following:
You can check it by another ways using the regex operator =~ as given below:
#!/bin/bash VAR='Lion is a King of forest.' if [[ $VAR =~ .*Lion.* ]]; then echo "It's contains." fi
The period followed by an asterisk
.* matches zero or more occurrences any character except a newline character.
Check if a String is Empty
It’s a common requirement to check that whether a variable is an empty string or not. Use the
-z operators to check it:
#!/bin/bash VAR='' if [[ -z $VAR ]]; then echo "String is empty." fi
String is empty.
#!/bin/bash VAR='Linuxize' if [[ -n $VAR ]]; then echo "String is not empty." fi
String is not empty.
Comparing Strings with the Case Operator
You can use the case statement to compare strings, instead of using the test operators:
#!/bin/bash VAR="Arch Linux" case $VAR in "Arch Linux") echo -n "Linuxize matched" ;; Fedora | CentOS) echo -n "Red Hat" ;; esac
You learned how to compare two strings in bash scripts.
If you have any questions or feedback, feel free to leave a comment.