created on | January 18, 2022 |
In statements like:
there should be a space before and after ‘[’ and ‘]’, otherwise ‘[’ and ‘]’ are not recognized.
traditional shell test command. available on all POSIX shells.
alternative
both forms are deprecated. The upgraded (new) version, which follows, should be used instead
upgraded version of test from ksh, also supported by bash and zsh. Supports regex. this is not POSIX.
again, there should be a space before and after ‘[[’ and ‘]]’, otherwise ‘[[’ and ‘]]’ are not recognized.
ksh extension that is also supported by bash and zsh. returns an exit code of zero (true) if the result of the arithmetic calculation is nonzero. this is not POSIX.
runs command in a subshell and acts on exit code
runs command and acts on exit code
deprecated (do not use this):
The better way is to use double brackets. Double brackets are special shell syntax to delimit conditional expressions. Inside double brackets, and are part of conditional expression syntax and do not have the meaning they have in the shell otherwise (taht is, combining commands, where the execution of a command depends on the exit code of the preceeding command).
Numeric tests can be combined with ‘(’ and ‘)’