HOWTO · Bash

String Comparison Operator in Bash

This article explains the string comparison in Bash using an if statement.

On this page

Use an if statement with Bash’s [ command to check whether two quoted strings have the same value. In [ "$first" = "$second" ], [ is the command name; the quoted values, =, and closing ] are separate arguments. Keep spaces between them so Bash passes each argument separately.

Compare Strings in Bash with if

This complete example compares two string variables and prints a message when they match. Replace either value to compare different text.

first="Hello"
second="Hello"
if [ "$first" = "$second" ]; then
    echo "Strings are equal"
else
    echo "Strings are different"
fi

The example was executed, and its stdout was:

Strings are equal

The comparison is handled by Bash’s [ command; if runs the first branch when that command succeeds and the else branch otherwise. Quoting each variable expansion keeps each value as one test argument, including when a value contains spaces or is unset. The article displays the captured stdout above so you can see which branch runs for these values.

Set the variables before the condition when the values come from earlier steps in a script. Assignment has no spaces around =, as in first="Hello there"; the spaces around the comparison operator belong inside the test. Quotes group each expansion into one argument, but they are not included in the value being compared. The comparison checks the full string, including its letter case and any leading or trailing spaces, so Hello and hello are different values.

Use = and == for String Equality

Inside [ ... ], use = to test whether two strings are equal. Bash also accepts == for string equality in this command, so [ "$first" == "$second" ] is an alternative condition. These operators compare the values on their left and right; the assignment operator in first="Hello" instead stores a value in a variable.

Keep the spaces around the operator. In [ "$first" = "$second" ], [ is the command name, and each quoted value, the operator, and the closing ] are separate arguments. Those spaces are required because they tell the shell where one argument ends and the next begins. For example, [ "$first"="$second" ] supplies one nonempty string to test rather than two values and an equality operator, so it does not perform the intended comparison.

For ordinary string equality in this Bash test, = is the clearest choice and == is also accepted. Pick one form and use it consistently in a script; do not confuse either comparison operator with the = used to assign a variable. The condition is true only when both values match exactly. If your program should ignore capitalization or trim surrounding spaces, transform the values first and compare the resulting strings; the equality test itself does not normalize them.

Test for Unequal Strings with !=

Use != in the same position to test whether the values differ. This standalone example prints the matching branch for its two values:

first="Hello"
second="World"
if [ "$first" != "$second" ]; then
    echo "Strings are different"
else
    echo "Strings are equal"
fi
Strings are different

The then branch runs when the strings differ; otherwise, Bash runs else. Keep both values quoted and preserve the spaces around != just as you would for =. To apply the pattern to user input or command output, assign each value first, then compare the variables with the operator that matches the condition you need.

Use this form when the next action depends on detecting a change, such as reporting that two names or configuration values do not match. The else branch is useful for the complementary case, so readers of the script can see what happens for either result. As with equality, quoting keeps a value containing spaces together as one test argument. Keep the operator between the two quoted values and leave a space before the final ]; this preserves the argument structure Bash expects.