I get what I expected when doing this in bash
:
[ "a" == "a" ] && echo yes
It gave me yes
.
But when I do this in zsh
, I get the following:
zsh: = not found
Why does the same command (/usr/bin/[
) behave differently in different shells?
bashtestzsh
I get what I expected when doing this in bash
:
[ "a" == "a" ] && echo yes
It gave me yes
.
But when I do this in zsh
, I get the following:
zsh: = not found
Why does the same command (/usr/bin/[
) behave differently in different shells?
Best Answer
It's not
/usr/bin/[
in either of the shells. In Bash, you're using the built-intest
/[
command, and similarly in zsh.The difference is that zsh also has an
=
expansion:=foo
expands to the path to thefoo
executable. That means==
is treated as trying to find a command called=
in yourPATH
. Since that command doesn't exist, you get the errorthat you saw (and in fact, this same thing would happen even if you actually were using
/usr/bin/[
).You can use
==
here if you really want. This works as you expected in zsh:because the quoting prevents
=word
expansion running. You could also disable theequals
option withsetopt noequals
.However, you'd be better off either:
=
, the POSIX-compatible equality test; or[[
conditionals with==
in both Bash and zsh. In general,[[
is just better and safer all around, including avoiding this kind of issue (and others) by having special parsing rules inside.