4

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 =?

15

1 Answer 1

7

= is the same as == when inside [[...]]. As per the more recent man page, under SHELL GRAMMAR > Compound Commands > [[ expression ]]:

The = operator is equivalent to ==

and further down, under CONDITIONAL EXPRESSIONS:

string1 == string2
string1 = string2
        True  if  the  strings  are equal.  = should be used with the test command
        for POSIX conformance. When used with the [[ command, this performs pattern
        matching as described above (Compound Commands).

bash info page:

enter image description here

2
  • 5
    Screenshot of a man page? Really?
    – jasonwryan
    Jan 7, 2015 at 22:28
  • @jasonwryan My bad! Looks like the info docs are updated more often than the auto-generated online docs.
    – muru
    Jan 7, 2015 at 22:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .