Binary Operators
The && and || operators each take two operands (making them binary). We will refer to the operand appearing on the left of the operator as the lvalue and the operand on the right the rvalue.
The exit status of the lvalue command determines, depending on which operator you use, if the rvalue command is executed.
lvalue exit status
rvalue command
zero (SUCCESS)
Not executed
non-zero (FAILURE)
Not Executed
The && operator acts as a logical AND between two commands and the || operator acts as a logical OR.
Any command, group of commands, or sub-shell can be used as an operand. Below are some examples:
1 #!/bin/sh
2 [ -e /proc/cmdline ] || {
3 echo "/proc/cmdline: No such file or directory"
4 exit 1
5 }
6 { cat /etc/redhat-release && ls -l /dev/sda; } || exit 1
7 year=$( date +%Y ) || year=1979
Lines 2-5 demonstrate a multi-line rvalue grouped by braces.
Line 6 demonstrates an lvalue grouped by braces.
Line 7 demonstrates that an assignment lvalue. The exit status of an assignment is that of the last command executed in the sub-shell (date +%Y). If executing date +%Y results in anything but success we would assign a default year of 1979.
Copy link