AND, OR and negation
command1 && command2 runs command2 only if command1 succeeds. command1 || command2 runs command2 only if command1 fails. These command lists short-circuit: the skipped command does not run. ! command negates a pipeline status (zero becomes 1; nonzero becomes zero). At the command-list level, && and || have equal precedence and associate left to right. In an if condition, combine independent tests with these list operators. The status of an AND or OR list is the status of the last command actually executed. These operators are not bitwise arithmetic operators.
Practical examples
Run each example separately in Bash 5.2 in a fresh empty practice directory. Tests use default shell options and C locale. Output is shown separately; do not enter it as code.
Short circuit
false && printf '%s\n' skippedtrue && printf '%s\n' runfalse || printf '%s\n' fallbackExpected stdout:
runfallbackThe second command is selected by the first status.
Combine checks
role=editorcount=3if [ "$role" = editor ] && [ "$count" -gt 0 ]; then printf '%s\n' permittedfiExpected stdout:
permittedBoth independent tests must succeed.
A failed second command
true && false || printf '%s\n' fallback! trueprintf '%s\n' "$?"Expected stdout:
fallback1The fallback runs because false fails; negating true gives status 1.
Common mistakes
A && B || C is not a general if/else replacement: C can run because B failed even if A succeeded. Prefer if/else for distinct branches.
Practice
Predict the printed messages for false && printf ..., false || printf ..., and ! true. Capture the final status of each separately.
Next: Bash double brackets and patterns