Site icon DesignLinux

How to Compare Strings in Bash

How to Compare Strings in Bash

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

Comparison operators are comparing the values and returns true or false. When comparing strings in Bash you can use the following operators:

Following points you should keep in mind while comparing strings:

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 = operator:

#!/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 == operator.

#!/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"
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:

It's contains.

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 -n and -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

Conclusion

You learned how to compare two strings in bash scripts.

If you have any questions or feedback, feel free to leave a comment.

Exit mobile version