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?
[ “$1” = “yes” ] && [ -r $2.txt ]
[ “$1” = “yes” -a $2 -lt 3 ]
Rule of thumb: Use
-o inside square brackets,
It's important to understand the difference between shell syntax and the syntax of the
|| are shell operators. They are used to combine the results of two commands. Because they are shell syntax, they have special syntactical significance and cannot be used as arguments to commands.
[ is not special syntax. It's actually a command with the name
[, also known as
[ is just a regular command, it uses
-o for its and and or operators. It can't use
|| because those are shell syntax that commands don't get to see.
But wait! Bash has a fancier test syntax in the form of
[[ ]]. If you use double square brackets, you get access to things like regexes and wildcards. You can also use shell operators like
> freely inside the brackets because, unlike
[, the double bracketed form is special shell syntax. Bash parses
[[ itself so you can write things like
[[ $foo == 5 && $bar == 6 ]].