Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The || (logical OR) operator is a control operator in Bash that executes a subsequent command only if the preceding command terminates with a non-zero exit status (failure). It utilizes short-circuit evaluation to dictate execution flow based on process return codes.
command1 || command2

Execution Mechanics

Bash evaluates the expression strictly from left to right:
  1. command1 is executed.
  2. If command1 returns an exit status of 0 (success), short-circuiting occurs. command2 is ignored, and the overall exit status of the list is 0.
  3. If command1 returns a non-zero exit status (failure), command2 is executed. The overall exit status of the list becomes the exit status of command2.

Chaining Commands

Multiple || operators can be chained. Due to left associativity, Bash evaluates the chain sequentially until one command returns 0.
command1 || command2 || command3
If command1 fails, command2 executes. If command2 succeeds, command3 is skipped. The chain terminates evaluation as soon as a single command yields a 0 exit status.

Precedence and Grouping

The || operator shares equal precedence with the && (logical AND) operator. When mixed in a single list without grouping, they are evaluated strictly left-to-right. To override default precedence, commands must be grouped using braces (executing in the current shell context) or parentheses (executing in a subshell context).

# Default left-to-right evaluation
command1 || command2 && command3


# Grouped evaluation overriding default precedence
command1 || { command2 && command3; }

Interaction with set -e (errexit)

When the set -e shell option is enabled, Bash normally exits immediately if a pipeline returns a non-zero status. However, if a failing command is part of an || list (specifically, any command except the final one in the chain), the errexit rule is suppressed. The shell will not exit, allowing the right-hand command to handle the failure state.
Master Bash with Deep Grasping Methodology!Learn More