Use this when callers must know whether automation succeeded.
Exit Codes · Error Handling
Exit codes in error handling tell the caller whether automation succeeded: 0 for success, non-zero for distinct failures. CI pipelines and schedulers rely on them to mark jobs as failed.
1. Exit codes signal results to callers (CI, schedulers).
2. 0 success, non-zero failure; document specific codes.
3. Propagate failures (set -e, pipefail).
4. Check native commands.
5. Test.
run → exit code → caller 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 ambiguous target identity, stale inputs, partial success, unsafe retry, permission drift and confusing tool completion with target-evidence of the actual state, 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.
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 means success, non-zero means failure.
- Use distinct codes for distinct failures.
- CI and schedulers rely on exit codes.
echo $?— Bash$LASTEXITCODE— PowerShell native- Documented exit code meanings — contract
ReferenceImportant terms & boundaries1 section · depth on demand
In depth
An exit code is the number a program returns to whatever started it: 0 means success, anything else means failure. Schedulers, CI pipelines and other scripts rely on it to decide whether to continue, so a script that prints an error but exits 0 hides its failure from every system around it.
| Code | Common meaning |
|---|---|
| 0 | Success |
| 1 | General error |
| 2 | Misuse / invalid arguments |
| 126 | Command not executable |
| 127 | Command not found |
| 130 | Interrupted (Ctrl+C) |
| 137 | Killed (SIGKILL, often out of memory) |
my_script.sh; echo "exit=$?"
set -o pipefail # a pipeline fails if any command in it fails
$LASTEXITCODE # native commands
exit 2 # return a specific code
Document the codes your own scripts return, especially when different failures need different handling.
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 Error Handling > 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.