by @codeexamples

How to compare two strings in Bash

Home » bash » How to compare two strings in Bash

To compare a two string in bash, we can use the if statement with equality comparison operator (==).

Here is an example:

str1="hello"
str2="hello"

# Equality Comparison
if [ "$str1" == "$str2" ]; then
    echo "Strings are matched"
else
    echo "Strings don't match"
fi

Output:

Strings are matched

Note: The spaces are important inside square brackets [ "$str1" == "$str2" ]