Both =
and ==
are operators. In some languages (like C) one is used to assign a value to a variable and the other to compare values (result of arithmetic expressions). In fact, both operators are exactly that inside Arithmetic Evaluation. A $((a=23))
is an assignment, a $((a==23))
is an Arithmetic Comparison.
$ echo "$((a=11)) $((a==23))" "$((a=23))" "$((a==23))"
11 0 23 1
But inside test constructs (all of test and […] and [[…]]) both operators are intended to mean the same and perform the same operation.
So, all of this options:
test "$a" = "$b"
[ "$a" = "$b" ]
[[ "$a" = "$b" ]]
test "$a" == "$b"
[ "$a" == "$b" ]
[[ "$a" == "$b" ]]
Are equivalent inside bash to test binary equality (variables quoted). If the right variable is unquoted it may be interpreted as a pattern and matched accordingly: as a pattern not as a literal string.
The quoted operators \=
and \==
are also equivalent when used in test and […]
. But the quoted operator \==
fails inside [[…]]
.
For other shells the results are varied (correct result should be Y -
(true false), an exit code different from 0 (true) and 1 (false) is reported as failure wih ¤
). Some shells fail with - -
(the exit code is always 1).
| dash ksh bash zsh
test a = "$b" | Y - Y - Y - Y -
[ a = "$b" ] | Y - Y - Y - Y -
[[ a = "$b" ]] | ¤ ¤ Y - Y - Y -
test a == "$b" | ¤ ¤ Y - Y - - -
[ a == "$b" ] | ¤ ¤ Y - Y - - -
[[ a == "$b" ]] | ¤ ¤ Y - Y - Y -
test a \= "$b" | Y - Y - Y - Y -
[ a \= "$b" ] | Y - Y - Y - Y -
[[ a \= "$b" ]] | ¤ ¤ Y - - - - -
test a \== "$b" | ¤ ¤ Y - Y - Y -
[ a \== "$b" ] | ¤ ¤ Y - Y - Y -
[[ a \== "$b" ]] | ¤ ¤ Y - - - - -
All options work in ksh, quoted operators fail in bash and zsh (inside [[…]]
) and the unquoted \=
and \==
also fail in zsh (outside of [[…]]
).