How to compare two strings in unix shell script

In this tutorial, let us discuss how to compare two string in the shell script.


Input

shell shell

Output

Equal


Input

shell bash

Output

Un Equal




shell script to compare two strings

Example

#shell script to compare two strings

read -p "Enter two strings: " str1 str2

if [ $str1 == $str2 ]
then
    echo "Equal"
else
    echo "Un Equal"
fi




Output

Enter two strings: shell bash

Un Equal


Enter two strings: shell shell

Equal




Compare variable value with string in shell script

Example

read -p "Enter a string: " str1

if [ $str1 == "linux" ]
then
    echo "linux"
elif [ $str1 == "unix" ]
then
    echo "unix"
else
    echo "Neither linux nor unix"
fi




Output

Enter a string: linux

linux


Enter a string: unix

unix


Enter a string: one

Neither linux nor unix


Useful Resources

To learn more shell script examples, you can visit the link