• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Compare Strings in Bash

Written by Admin, Updated On October 25, 2020
bash, terminal
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:

  • You can check like string1 = string2 and string1 == string2 and 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.
  • 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.
  • -z string – True if the string length is zero.
  • -n string – 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 = 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.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article Bash Case Statement
Next Article   Gunzip Command in Linux

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install PHP 8.2 on Debian 11
    How to Install PHP 8.2 on Debian 11 Linux February 24, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook