Can someone please explain the difference between the &&
, ||
, -a
, and -o
Unix operators?
What are the restrictions on the usage of both types?
Is it simply that the &&
and ||
operators should be used when using flags in the condition?
As in:
[ "$1" = "yes" ] && [ -r $2.txt ]
versus:
[ "$1" = "yes" -a $2 -lt 3 ]
-a and -o are the older and/or operators for the test command. && and || are and/or operators for the shell. So (assuming an old shell) in your first case,
[ "$1" = 'yes' ] && [ -r $2.txt ]
The shell is evaluating the and condition. In your second case,
[ "$1" = 'yes' -a $2 -lt 3 ]
The test command (or builtin test) is evaluating the and condition.
Of course in all modern or semi-modern shells, the test command is built in to the shell, so there really isn't any or much difference. In modern shells, the if statement can be written:
[[ $1 == yes && -r $2.txt ]]
Which is more similar to modern programming languages and thus is more readable.
Source: Stackoverflow.com