bash compare strings

Dealing with strings is part of any programming language. When working with Bash scripts you will need to compare the value of two strings to determine whether they are equal or not.

String comparison is very useful and one of the most used statements in Bash scripts and its crucial to understand and know how to use it.

In this tutorial we will show you different use cases for string comparison in Bash Linux.

Basic Syntax of String Comparison

Basic syntax of string comparison is shown below:

if [ conditions/comparisons]
then
commands
fi

Check If Two Strings are Equal or Not Equal

In this section, we will learn how to check if two strings are equal or not equal in Bash script.

Check If Two Strings are Equal

You can use equal operators = and == to check if two strings are equal. You must use single space before and after the == and = operators.

In this example, we will use (=) operator with if statement to check if the strings are equal or not and returns the output.

First, create a test.sh script as shown below:

nano test.sh

Add the following code:

#!/bin/bash
str1="Linux";
str2="Linux";
if [ $str1 = $str2 ]
then
echo "Strings are same";
else
echo "Strings are not same";
fi

Save and close the file when you are finished. Then, run this script with the following command:

bash test.sh

You should see the following output:

Strings are same

You can also use the following bash script to take input from the user and check if given strings are equal or not.

First, create a new test.sh file as shown below:

nano test.sh

Add the following code:

#!/bin/bash
read -p "Enter Your First String: " str1
read -p "Enter Your Second String: " str2
if [ $str1 = $str2 ]
then
echo "Strings are same";
else
echo "Strings are not same";
fi

Save and close the file when you are finished. Then, run the script as shown below:

bash test.sh

You will be asked to enter first and second string as shown below:

Enter Your First String: Linux
Enter Your Second String: Ubuntu
Strings are not same

 

== Operator to Compare Strings

You can also use (==) operator to compare two strings. Let’s see another example:

nano test.sh

Add the following code:

#!/bin/bash
str1="Hitesh";
str2="Rajesh";
if [[ $str1 == $str2 ]]
then
echo "Strings are same";
else
echo "Strings are not same";
fi

Save and close the file when you are finished. Then, run this script with the following command:

bash test.sh

You should see the following output:

Strings are not same

 

Check If Two Strings are Not Equal (!=)

Bash also provides the negation operator to use “if not equal” condition in bash scripts. You can use (!=) operator to check when both strings are not equal.

Let’s create a new test.sh script as shown below:

nano test.sh

Add the following code:

#!/bin/bash
str1="Linux"
str2="Windows"
if [ "$str1" != "$str2" ]
then
echo "Not Equal Strings"
else
echo "Equal Strings"
fi

Save and close the file when you are finished. Then, run this script with the following command:

bash test.sh

You should see the following output:

Not Equal Strings

Check If String is Empty

Bash also allows you to check whether the string is empty.

First, create another test.sh script to check that the string is empty.

nano test.sh

Add the following code:

#!/bin/bash
str1=""
if [ -z $str1 ]
then
echo "String is empty"
else
echo "String is not empty"
fi

Save and close the file when you are finished. Then, run the script as shown below:

bash test.sh

You should see the following output:

String is empty

Next, create an another test.sh script to check that the string is not empty.

nano test.sh

Add the following code:

#!/bin/bash
str1="Linux"
if [ -z $str1 ]
then
echo "String is empty"
else
echo "String is not empty"
fi

Save and close the file when you are finished. Then, run the script as shown below:

bash test.sh

You should see the following output:

String is not empty

Check If First String is Greater Than or Less Than Second String (\>) or (\<)

You can use greater than (\>) or less then (\<) operators to check if the first string is greater than or less then the second string.

First, create a test.sh script to check if the first string is greater than the second string.

nano test.sh

Add the following code:

#!/bin/bash
str1="Webservertalk"
str2="Webserver"
if [ $str1 \> $str2 ]
then
echo "$str1 is greater then $str2"
else
echo "$str1 is not greater then $str2"
fi

Save and close the file when you are finished. Then, run the script as shown below:

bash test.sh

You should see the following output:

Webservertalk is greater then Webserver

Next, create a test.sh script to check if the first string is less than the second string.

nano test.sh

Add the following code:

#!/bin/bash
str1="welcome"
str2="welcometowebserver"
if [ $str1 \< $str2 ]
then
echo "$str1 is less then $str2"
else
echo "$str1 is not less then $str2"
fi

Save and close the file when you are finished. Then, run the script as shown below:

bash test.sh

You should see the following output:

welcome is less then welcometowebserver

Conclusion

In the above tutorial, we learned how to compare two strings in bash scripts. We hope you have learned how to compare two strings in bash. Feel free to leave comments below if you have any questions.