Skip to content

How to compare Strings in Bash?

When writing scripts in just about any language, you’d often need to compare two or more strings to check if they’re equal or not. In programming terms, two strings are considered equal when they have the same length and same sequence of characters. 

In this article, we’re going over all the string comparisons you might have to do when churning out Bash scripts. 

Also read: How to install Steam on Linux?


Bash comparison operators

Comparison operators compare two values in boolean — the answer is always either true or false. You can use the following operators to compare strings in Bash.

  • string1 = string2 or string1 == string2: The equality operator returns true if both the operands are the same. 
    • Use the operator with the test [ command.
    • Use the == operator with the [[ command for pattern matching.
  • string1 != string2: The inequality operator returns true if the given operands are not equal, in this case, the given two strings. 
  • string1 =~ regex: The regex operator will return true if string1 matches the regular expression on the right. 
  • string1 > string2: The greater than operator will return true if the operator on the left is greater than the one on the right. In this case, it’ll return true if string1 is greater than string2.
  • string1 < string2: This operator works in the exact opposite way from the previous operator. The operator will return true if string1 is less than string2.
  • -z string – Returns true if the string length is zero.
  • -n string – Returns true if the string length is non-zero.

However, when using these operators, be sure to keep the following things in mind. 

  • Always use a blank space between the binary operator and the operands, in this case, the strings.
  • Use double quotes around the variable names to avoid any word spilling or globbing.
  • Since Bash doesn’t segregate variables by “type”, a given variable can be treated as a string or an integer depending on the context. 

Also read: 25 essential Linux Terminal commands


How to check if two strings are equal?

To check if two strings are equal or not, you’ll have to use the if command along with the operator. 

#!/bin/bash

VAR1="String1"
VAR2="String2"

if [ "$VAR1" = "$VAR2" ]; then
    echo "Strings are equal."
else
    echo "Strings are not equal."
fi

The above snippet will output Strings are not equal as VAR1 and VAR2 contact different strings. 

If you want, you can even have the user input the strings. Except for this time, you’re going to have to use the == operator and the [[ command. 

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

Lastly, you can also skip the if statement and use the logical and (&&) operator to compare the twos strings 

[[ "string1" == "string2" ]] && echo "Equal" || echo "Not equal"

Also read: How to change MAC address on Windows and Linux?


How to check a string for substrings?

Checking for a substring inside of a string requires the use of the wildcard operator. Here’s how.

#!/bin/bash

VAR='Is the word in this sentence?'
if [[ $VAR == *"word"* ]]; then
  echo "Yes it is."
fi

Alternatively, you can also use the regex (=~) operator. 

#!/bin/bash

VAR='Is the word in this sentence?'
if [[ $VAR =~ .*word.* ]]; then
  echo "Yes it is."
fi

How to check if a string is empty?

If you’re looking to check if a string is empty, try using the -n and -z operators.

#!/bin/bash

VAR=''
if [[ -z $VAR ]]; then
  echo "String is empty."
fi

This program will output String is empty as VAR1 is quite literally an empty string. 

Also read: How to install Kali Linux on Virtualbox?


How to compare strings in Bash with the case operator?

If test operators aren’t quite your thing, you can also use the case statement to compare two strings.

#!/bin/bash

VAR="String1"

case $VAR in

  "String1")
    echo -n "Match found"
    ;;

  MainString| MainString2)
    echo -n "MainNS"
    ;;
esac

The above script will output Match found as VAR matches the string in question. 


How to compare strings lexicographically?

In lexicographic comparisons, two strings are compared alphabetically by comparing the characters in one string sequentially from left to right. 

You can use the following script to compare two strings lexicographically.

#!/bin/bash

VAR1="String1"
VAR2="String2"

if [[ "$VAR1" > "$VAR2" ]]; then
    echo "${VAR1} is lexicographically greater then ${VAR2}."
elif [[ "$VAR1" < "$VAR2" ]]; then
    echo "${VAR2} is lexicographically greater than ${VAR1}."
else
    echo "Strings are equal"
fi

Also read: How to delete Files in Linux?

Hello There!

If you like what you read, please support our publication by sharing it with your friends, family and colleagues. We're an ad-supported publication. So, if you're running an Adblocker, we humbly request you to whitelist us.

We may earn a commission if you buy something from a link on this page. Thanks for your support.







>