TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
&& (logical AND) operator is a control operator used to chain commands based on their exit status. It implements short-circuit evaluation, executing the right-hand command strictly if, and only if, the left-hand command terminates with an exit status of zero (success).
Execution Mechanics
Bash parses the entire command line or script block for syntax before any execution begins. If no syntax errors are found, execution proceeds as follows:- Bash executes
command1and captures its exit status ($?). - If the exit status is
0(success), Bash proceeds to executecommand2. - If the exit status is non-zero (
1-255, indicating failure), Bash short-circuits the evaluation.command2is not executed.
Exit Status Propagation
The overall exit status of an&& command list is determined by the last command executed within that list:
- If
command1fails, the list returns the non-zero exit status ofcommand1. - If
command1succeeds, the list returns the exit status ofcommand2, regardless of whethercommand2succeeds or fails.
Interaction with errexit (set -e)
When the errexit option (set -e) is enabled, Bash normally exits immediately if a command yields a non-zero exit status. However, according to POSIX and Bash semantics, the errexit rule is ignored for any command in an AND-OR list except the last one. Consequently, if command1 fails, the overall && list returns a non-zero exit status, but this does not trigger set -e to halt the script. The script will only terminate if the final command in the chain (e.g., command2) is executed and fails.
Associativity and Chaining
The&& operator is left-associative and shares equal precedence with the || (logical OR) operator. When chaining multiple commands, evaluation proceeds strictly from left to right.
command3 is executed only if both command1 and command2 sequentially return an exit status of 0. A non-zero exit status at any point breaks the chain immediately.
Contextual Overloading
Beyond operating as a command list control operator,&& is overloaded in specific Bash evaluation contexts:
- Extended Test Construct (
[[ ]]): Acts as a logical AND operator between conditional expressions. It evaluates the results of specific conditional test operators (such as string comparisons,-z, or-f), rather than generic boolean “truthiness”.
Master Bash with Deep Grasping Methodology!Learn More





