I just wondered about some people writing
if [[ <expr> ]]; then ...
in answers here when I out of habit would use
if [ <expr> ]; then ...
Doing a quick research reveals that [[ ]]
seems to be a shell builtin introduced once by the Korn shell and also provided by Bash (and others perhaps), while [ ]
is just a shortcut for the external command test
and thus equal to
if test <expr>; then ...
Now,
- Are there differences between the individual implementations? (Leaving aside busybox as a stripped-down shell/test implementation.)
Edit: Best answers by https://unix.stackexchange.com/a/184293/157411 and https://unix.stackexchange.com/a/56666/157411. - Are there cases where one should or must be preferred over the other? (Except when one of them isn't available, e. g. in a very restricted environment.)
Edit: Best answer by https://unix.stackexchange.com/a/56666/157411; in short if you would like to avoid quoting issues and use the common operators instead of switches use[[ ]]
. - Which one to use for maximum compatibility? (And I hope this is unambiguously answerable...)
Edit: Best answer by https://unix.stackexchange.com/a/56674/157411; in short if you want to be compatible usetest
or[ ]
.
Edit: Related questions and answers: