Skip to main content
A conditional OR list is a sequence of one or more pipelines separated by the || control operator. It implements short-circuit evaluation, executing a subsequent command in the sequence strictly if the preceding AND/OR list evaluates to a non-zero (failure) exit status.

Execution Mechanics

The shell evaluates a conditional OR list from left to right.
  1. command1 is executed, and the shell captures its exit status.
  2. If command1 returns an exit status of 0 (success), the shell short-circuits the evaluation. command2 and any subsequent commands in the contiguous OR list are skipped and not executed.
  3. If command1 returns a non-zero exit status (failure), the shell proceeds to execute command2.
  4. This evaluation model propagates down the chain until a command returns 0 or the end of the list is reached.

Exit Status

The overall exit status of a conditional OR list is determined by the exit status of the last command executed within that list.
  • If any command in the sequence evaluates to 0, the list terminates immediately, and the exit status of the entire list is 0.
  • If all commands in the sequence fail, the list evaluates to the non-zero exit status of the final command in the chain.

Precedence and Associativity

The || control operator shares equal precedence with the conditional AND operator (&&). Both operators are strictly left-associative. When || and && are mixed in a single un-grouped command string, the shell parses and evaluates them sequentially from left to right. Because of left-associativity, the execution of a subsequent command depends on the exit status of the entire preceding AND/OR list evaluated so far, not just the single pipeline immediately preceding the operator.
In the structure above, if command1 succeeds (returns 0), command2 is skipped. However, the exit status of the preceding list (command1 || command2) is 0. Consequently, the && operator sees a successful preceding list, causing command3 to execute. To override default left-associativity and enforce specific evaluation precedence, commands must be explicitly grouped using compound commands, such as subshells ( ... ) or brace groups { ...; }.
Tired of Poor Bash Skills? Fix That With Deep Grasping!Learn More