POSIX test
(or [ ... ]
) only knows about the one with a single equal sign:
s1 = s2
True if the strings s1 and s2 are identical; otherwise, false.
But Bash accepts the double equal sign too, though the builtin help doesn't admit to that (the manual does):
$ help test | grep -A1 =
STRING1 = STRING2
True if the strings are equal.
STRING1 != STRING2
True if the strings are not equal.
As for other shells, it depends. Well, particularly Dash is the stubborn one here:
$ dash -c '[ x == x ] && echo foo'
dash: 1: [: x: unexpected operator
but
$ yash -c '[ x == x ] && echo foo'
foo
$ busybox sh -c '[ x == x ] && echo foo'
foo
$ ksh93 -c '[ x == x ] && echo foo'
foo
In zsh
, =something
is a filename expansion operator that expands to the path of the something
command if found in $PATH
unless the equals
option is turned off (like when emulating other shells), and that also applies to ==
or =~
when found in arguments to simple commands such as test
or [
so at least the leading =
must be quoted there:
$ zsh -c 'echo =ls'
/usr/bin/ls
$ zsh +o equals -c 'echo =ls'
=ls
$ zsh --emulate ksh -c 'echo =ls'
=ls
$ zsh -c '[ x == x ] && echo foo'
zsh:1: = not found
$ zsh -c '[ x "==" x ] && echo foo'
foo
The external test
/[
utility from GNU coreutils on my Debian supports ==
(but the manual doesn't admit that), the one on OS X doesn't.
So, with test
/[ .. ]
, use =
as it's more widely supported.
With the [[ ... ]]
construct, both =
and ==
are equal (at least in Bash) and the right side of the operator is taken as a pattern, like in a filename glob, unless it is quoted. (Filenames are not expanded within [[ ... ]]
)
$ bash -c '[[ xxx == x* ]] && echo foo'
foo
But of course that construct isn't standard:
$ dash -c '[[ xxx == x* ]] && echo foo'
dash: 1: [[: not found
$ yash -c '[[ xx == x* ]] && echo foo'
yash: no such command ‘[[’
And while Busybox has it, it does't do the pattern match:
$ busybox sh -c '[[ xx == xx ]] && echo yes || echo no'
yes
$ busybox sh -c '[[ xx == x* ]] && echo yes || echo no'
no
=
and==
are the same (inside test constructs). – Jul 26 '17 at 20:05[[...]]
– Stéphane Chazelas Jul 27 '17 at 06:22