Why "never use the -a
or -o
operator"?
Because they may be amibiguous and hence they're not POSIX-compliant:
The XSI extensions specifying the -a and -o binary primaries and the '(' and ')' operators have been marked obsolescent. (Many expressions using them are ambiguously defined by the grammar depending on the specific expressions being evaluated.) Scripts using these expressions should be converted to the forms given below. Even though many implementations will continue to support these obsolescent forms, scripts should be extremely careful when dealing with user-supplied input that could be confused with these and other primaries and operators. Unless the application developer knows all the cases that produce input to the script, invocations like:
test "$1" -a "$2"
should be written as:
test "$1" && test "$2"
How can I do "use several [
commands and the &&
and ||
shell operators)"?
By doing multiple tests and chaining them using said operators; for example:
[ 0 -eq 0 -a \( 0 -eq 1 -o 1 -eq 1 \) ]
could be rewritten as the equivalent:
[ 0 -eq 0 ] && ([ 0 -eq 1 ] || [ 1 -eq 1 ])
or, better:
[ 0 -eq 0 ] && { [ 0 -eq 1 ] || [ 1 -eq 1 ]; }
(
and)
the operators that group commands? If they are obsolete, what are their replacement? (2) Shouldtest "$1" -a "$2"
be replaced bytest "$1" && test "$2"
, or by((test "$1" && test "$2"))
? Don't we need the((..))
? – Tim Mar 19 '16 at 08:22()
and{}
as a replacement. (2) The first one;(())
is used for arithmetic expansion, here you want to test the exit status of the chain of tests, so either group the tests in a subshell()
or in the current shell{}
. – kos Mar 19 '16 at 08:35(
and)
operators is when you're using-a
or-o
. Since the latter are obsolescent, the former are obviously no longer needed either. – Barmar Mar 23 '16 at 17:45