man bash
:
[[ expression ]]
[...] Expressions are composed of the primaries described below under CONDITIONAL EXPRESSIONS. Word splitting and pathname expansion are not performed on the words between the [[ and ]];
[...] When the == and != operators are used, the string to the right of the operator is considered a pattern and matched according to the rules described below under Pattern Matching.
In the whole section the case of a single =
is not mentioned.
CONDITIONAL EXPRESSIONS
[...]
string1 == string2
string1 = string2
True if the strings are equal. = should be used with the test command for POSIX conformance.
From this description I would expect that
[[ a = $cmpstring ]]
checks for equal strings and
[[ a == $cmpstring ]]
checks for a pattern match. But that is not the case:
> [[ a == ? ]]; echo $?
0
> [[ a = ? ]]; echo $?
0
> [[ a == "?" ]]; echo $?
1
Do I misunderstand something or has the bash man page just forgotten to mention =
?
=
info bash
would say).=
and==
are both used for "string equality" (ref conditional expressions) but within[[...]]
, they do pattern matching instead (ref conditional constructs)Edition 4.2, last updated 28 December 2010, of The GNU Bash Reference Manual, for Bash, Version 4.2
).