Use this when scripts must report success or failure correctly.
Exit Codes · Bash Automation
Exit codes are the number a command returns: 0 for success and 1–255 for failure, available in $?. Scripts should exit non-zero on errors so callers and schedulers detect failures.
1. Exit 0 = success; non-zero = failure.
2. Check codes explicitly for important commands.
3. set -e has exceptions (conditions, pipelines without pipefail).
4. Document meaningful codes.
5. Callers (cron, systemd, CI) rely on them.
command → exit code → decision
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Verification path Verify boundary
The negative fixture should deliberately exercise risks such as unquoted expansion, globbing, pipeline status loss, subshell scope, set -e misconceptions, signal races, temporary-file leakage and destructive path ambiguity, choosing the cases most relevant to process exit status as a contract, reserved/application-specific codes, pipeline/wrapper propagation and keeping stderr/output interpretation separate.
Step 2 Common mistakes Narrow the fault
- Scripts that always exit 0.
set -esurprises insideifand pipelines — test them.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- 0 is success; non-zero is failure.
- $? holds the last exit code.
- Exit non-zero on errors.
echo $?— last exit codeset -e— exit on errorexit 3— explicit code
ReferenceWhy it matters1 section · depth on demand
What it is
Every command returns an exit code: 0 for success, non-zero for failure. Scripts should check them and exit non-zero on failure so schedulers and CI notice.
ReferenceImportant terms & boundaries2 sections · depth on demand
In depth
Exit codes are how Bash scripts report success (0) or failure (non-zero) to callers, schedulers and CI. A script's exit code is that of the last command run unless it exits explicitly, which can hide failures.
#!/usr/bin/env bash
set -euo pipefail
readonly E_USAGE=2 E_UNREACHABLE=3
[[ $# -eq 1 ]] || { echo "usage: $0 host" >&2; exit $E_USAGE; }
ping -c1 -W2 "$1" >/dev/null || exit $E_UNREACHABLE
if ! out=$(ssh "$1" 'show version' 2>&1); then
echo "ssh failed: $out" >&2; exit 1
fi
With set -e, commands in if conditions, || and && lists do not stop the script, which is how expected failures are handled explicitly. Document custom exit codes so callers can react to specific failures.
Example
set -euo pipefail
if ! ping -c1 -W2 "$host" >/dev/null; then
echo "$host down" >&2; exit 1
fi
echo $? # exit code of the last command
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Bash Automation > Exit Codes. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://www.gnu.org/software/bash/manual/bash.html · confidence 94%Deep reference
Healthy vs suspicious evidence
Output interpretation
For process exit status as a contract, reserved/application-specific codes, pipeline/wrapper propagation and keeping stderr/output interpretation separate, prefer typed fields and stable IDs over human presentation text.