Shell & Utilities - Single UNIX Specifications warns to not use them if script's input may be such strings.
I didn't find where it tells they're deprecated.
This is not the same, for the single bracket version; there should be single = signs and double quotes around the left variable and the negation operator should work on the entire logical comparison, so one would need to use grouping to accomplish this:
if ! ( [ "$1" = "foo" ] || [ "$1" = "bar" ] )
then
The use of parentheses means using a subshell, so I think it is more efficient to use block grouping:
if ! { [ "$1" = "foo" ] || [ "$1" = "bar" ] ;}
then
Or use the logical equivalent:
if [ "$1" != "foo" ] && [ "$1" != "bar" ]
then
The equivalent using the features of test that are obsoleted in the POSIX specification would be:
if [ ! \( "$1" = "foo" -o "$1" = "bar" \) ]
then
-or-
if ! [ "$1" = "foo" -o "$1" = "bar" ]
then
---------- Post updated at 11:42 ---------- Previous update was at 09:24 ----------
if ! { [ "$var" = a ] || [ "$var" = b ] || [ "$var" = b ] ;}
then
If you are writing a Korn shell script and do not need POSIX compliancy you can use this:
if ! [[ $var == a || $var == b || $var == c ]]
then
To answer post #1 :
The correct syntax in your context is:
if [ 1 -eq 1 -a \( 2 -eq 2 -o 2 -eq 3 \) ]
then
echo "true"
else
echo "false"
fi
Note that the parentheses are escaped because we want to give them to "test" and not start a subshell.
@Scrutinizer
The word is "depreciated" not "deprecated" !
Anyway there is no way that the "-a" and "-o" syntax or the brackets will be "depreciated" no matter how often the Posix police mention it.