Linux shell programming string compare syntax Linux shell programming string compare syntax shell shell

Linux shell programming string compare syntax


The single equal is correct

string1 == string2

string1 = string2

True if the strings are equal. ‘=’ should be used with the test command for POSIX conformance

NAME="rafael"USER="rafael"if [ "$NAME" = "$USER" ]; then    echo "Hello"fi


In general, the = operator works the same as == when comparing strings.

Note:The == comparison operator behaves differently within a double-brackets test than within single brackets.

[[ $a == z* ]]   # True if $a starts with an "z" (pattern matching).[[ $a == "z*" ]] # True if $a is equal to z* (literal matching).[ $a == z* ]     # File globbing and word splitting take place.[ "$a" == "z*" ] # True if $a is equal to z* (literal matching).

source: http://tldp.org/LDP/abs/html/comparison-ops.html