OR list
Two commands joined by ||, where the second runs only if the first fails.
In more detail
cd dir || exit 1 is the standard guard against carrying on in the wrong directory. Under set -e, a failing command on the left of || does not stop the script, because the failure has been handled.
Example
cmd || { echo 'what failed' >&2; exit 1; }Related terms
- AND list — Two commands joined by &&, where the second runs only if the first succeeds.
- set -e — The shell option that makes a script exit as soon as a command fails, instead of carrying on.
- exit code — A number a process returns when it finishes: 0 means success, anything else means something went wrong.
Taught in
- The Unquoted Variable: Splitting And Globbing — Bash Scripting & Command Line Automation
- Exit Codes, And What Success Means — Bash Scripting & Command Line Automation
- Conditionals: test, Brackets, Double Brackets, case — Bash Scripting & Command Line Automation
- set -e, set -u And pipefail: Promises And Traps — Bash Scripting & Command Line Automation
- Permissions And Ownership — Bash Scripting & Command Line Automation