[ACCEPTED]-Linux shell programming string compare syntax-compare

Accepted answer
Score: 23

The single equal is correct

string1 == string2

string1 3 = string2

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

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

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

Note: The == comparison 2 operator behaves differently within a double-brackets 1 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

Score: 4

These pages explain the various comparison 2 operators in bash:

On the second linked page, you 1 will find:

==

    is equal to

    if [ "$a" == "$b" ]

    This is a synonym for =.
Score: 1

you can take a look here or here. Personally, to 2 compare strings, I use case

case "$string1" in
  "$string2" ) echo "matched";;
  *) echo "not matched";;
esac

I do not have to 1 know which operator i should use

More Related questions