Use it to make scripts react to failures, or to understand why a script continued after an error.
Exit Codes
An exit code (exit status) is the number a command returns when it ends: 0 means success, 1–255 an error. $? holds the last one; &&, ||, if and set -e act on it.
1. Run the command, then echo $?.
2. 127 → not found (PATH); 126 → not executable.
3. 137 → killed (check journalctl -k | grep -i oom).
4. In a pipeline → ${PIPESTATUS[@]}.
5. Make scripts check: cmd || exit 1.
command ends → exit status set → $? updated → &&, ||, if or set -e decide what happens next
For Shell & Bash → Exit Codes, continue with the first unproven Topic check: Capture the relevant status immediately and consult the command/application contract for its meaning. If that boundary is healthy, advance to the adjacent check: For pipelines capture per-stage status and define whether pipefail is required. Preserve the same host/time/identity scope and rerun the original workload before accepting recovery.
Continue only after interpreting the current observation.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 Operational evidence path Verify boundary
process/builtin operation → application-defined exit status or signal → shell pipeline/list/control-flow semantics → $? / PIPESTATUS → caller decision → target/workload verification
Healthy evidence: status is captured from the correct command/stage, interpreted using its documented contract and corroborated by target/workload evidence where needed.
Step 2 Verification and output interpretation Verify boundary
Existing fast checks for this Topic. - Capture the relevant status immediately and consult the command/application contract for its meaning.
- For pipelines capture per-stage status and define whether pipefail is required.
- Test conditional/&&/||/! contexts because shell control flow can transform how statuses are interpreted.
- Pair status with effective-state/read-back and workload acceptance evidence for operations that change systems.
Verification chain. process/builtin operation → application-defined exit status or signal → shell pipeline/list/control-flow semantics → $? / PIPESTATUS → caller decision → target/workload verification
Step 3 Commands Verify boundary
cmd ; echo $?
false || echo failed
exit 3
Step 4 Failure modes and next checks Narrow the fault
Suspicious evidence: the status is overwritten, last-stage pipeline success hides upstream failure, or a generic zero/nonzero rule misclassifies application-defined outcomes.
Step 5 Common mistakes Narrow the fault
- Scripts that exit 0 after errors.
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.
Conventions: 1 general error, 2 misuse, 126 not executable, 127 command not found, 128+N killed by signal N (130 = Ctrl+C, 137 = SIGKILL, often OOM). Read the man page for a command's own codes (grep returns 1 for "no match").
bash -x script.sh— trace each expanded commandbash -n script.sh— syntax check onlyshellcheck script.sh— common bugs and quoting problemsecho $?— exit code of the last command
ReferenceWhat it is1 section · depth on demand
Core model
Capture status immediately because $? is overwritten by the next command. Define meaning per command/application rather than assuming every nonzero is fatal. For pipelines inspect PIPESTATUS/pipefail; for boolean lists/conditionals understand status inversion/short-circuiting; correlate status with target-state verification.
ReferenceImportant terms & boundaries5 sections · depth on demand
What this shows and what it does not
Exit-code evidence shows only the producer's reported status under the caller's shell semantics, but it does not show side effects or end-to-end success.
In depth
Exit codes and how to use them
Every command ends with a numeric exit status. 0 means success, and anything else means failure or a special condition. The bash(1) manual page defines the values the shell itself uses:
- 127 means the command wasn't found, and 126 that it was found but couldn't be executed.
- 128+N means the command was killed by signal N.
- Builtins return 2 for incorrect usage.
$?holds the status of the last command.
Reserved and conventional codes
| Code | Meaning |
|---|---|
| 0 | Success |
| 1 | General error |
| 2 | Misuse of a builtin; many tools use it for usage errors |
| 126 | Found but not executable (permissions, wrong format) |
| 127 | Command not found (PATH, a typo) |
| 130 | Killed by SIGINT (Ctrl-C): 128 + 2 |
| 137 | Killed by SIGKILL: 128 + 9 (often the OOM killer, or a container limit) |
| 143 | Killed by SIGTERM: 128 + 15 |
| 255 | ssh: the connection itself failed |
Tools that use exit codes meaningfully
| Tool | Codes |
|---|---|
grep | 0 = match, 1 = no match, 2 = error |
diff | 0 = same, 1 = different, 2 = trouble |
systemctl is-active | 0 = active, non-zero otherwise |
ping -c | 0 = replies, 1 = no reply, 2 = error |
test / [ ] | 0 = true, 1 = false |
Using them
| Pattern | Example |
|---|---|
| Branch on success | if grep -q ERROR app.log; then ...; fi |
| Run only on success | make && make install |
| Fallback | curl -fsS url || echo "fetch failed" >&2 |
| Save before it's overwritten | cmd; rc=$? |
| End a script with a code | exit 3 |
host$ ls /nonexistent; echo $?
ls: cannot access '/nonexistent': No such file or directory
2
host$ nosuchcmd; echo $?
bash: nosuchcmd: command not found
127
host$ /etc/hostname; echo $?
bash: /etc/hostname: Permission denied
126
host$ sleep 100 & kill -TERM $!; wait $!; echo $?
143
host$ grep -q root /etc/passwd; echo $?
0
host$ systemctl show report-api -p ExecMainStatus,Result
ExecMainStatus=137
Result=oom-kill
Reading 137
A service or container that ends with 137 was killed with SIGKILL. In systemd, Result=oom-kill confirms that the kernel's OOM killer, or a memory limit, did it. In that case, look at memory use and limits, not at the application's own error handling.
Documented example, not captured output.
How it works at runtime
Exit Codes is part of Shell & Bash → Exit Codes. Exit Codes are small integer process/shell statuses used to signal success or categories of failure to a parent shell; Bash also synthesizes statuses for builtins, pipelines, command-not-found and signal termination.
Topic-specific state model. Exit Codes are small integer process/shell statuses used to signal success or categories of failure to a parent shell; Bash also synthesizes statuses for builtins, pipelines, command-not-found and signal termination.
Interpretation rule. - 0 ≠ desired target state; it means only the command's defined success condition.
- Nonzero ≠ universally fatal; grep 1 can mean no match, diff 1 means differences, and applications define their own contracts.
- $? later ≠ status of an earlier command once another command has executed.
- 128+signal convention ≠ a portable complete explanation without signal/process evidence.
Quick reference
Exit codes: 0 means success, 1–255 failure. Conventions:
| Code | Meaning |
|---|---|
| 1 | General error |
| 2 | Misuse of builtin / bad usage |
| 126 | Found but not executable |
| 127 | Command not found |
| 128+N | Killed by signal N (130 Ctrl+C, 137 SIGKILL, 143 SIGTERM) |
cmd; echo $?
if grep -q pattern file; then …; fi
cmd || { echo 'failed' >&2; exit 1; }
systemctl status app | grep 'code=exited'
Commands differ: grep returns 1 for "no match" (not an error), diff returns 1 for "different" — handle these explicitly with set -e.
Limits and how it fails
Primary failure point. Exit-code evidence shows only the producer's reported status under the caller's shell semantics, but it does not show side effects or end-to-end success.
ReferenceConfiguration & state1 section · depth on demand
Configuration, persistence and lifecycle
Distribution/platform context. Shell Scripts owns orchestration/error handling; Pipes owns pipeline status composition; Signals owns termination mechanisms; this Topic owns status semantics.
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 Shell & Bash → 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%What changed and what may be affected
Recent technical edits are separated from ordinary page timestamps. Relationship impact is advisory: it identifies nearby canonical references to review, not automatic breakage.
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.
Quick lookup tables
Exit Codes operational reference
Topic-specific lookup boundaries, proof limits and next-step Evidence for Exit Codes.
| Lookup boundary | Topic-specific reference | Operational use |
|---|---|---|
| Scope / effective state | Exit Codes are small integer process/shell statuses used to signal success or categories of failure to a parent shell; Bash also synthesizes statuses for builtins, pipelines, command-not-found and signal termination. | Establish exactly what this Topic owns before crossing into another subsystem. |
| Fast verification | Pin host/distribution + shell/version/mode → identify cwd/mount namespace + input/path/stream → preserve quoting/expansion and final argv → map fd/pipeline/process boundaries → inspect object/variable/status/signal state → make one minimal reversible change → read back effective state → repeat original consumer/script/workload test. | Use the shortest evidence path first; a fast check is a boundary test, not blanket proof. |
| Verification chain | process/builtin operation → application-defined exit status or signal → shell pipeline/list/control-flow semantics → $? / PIPESTATUS → caller decision → target/workload verification | Trace requested/configured state through effective state to the observed workload result. |
| Failure boundary | Exit-code evidence proves only the producer's reported status under the caller's shell semantics; it does not independently prove side effects or end-to-end success. | Stop broad troubleshooting at the first boundary that is not supported by evidence. |
| Positive evidence | Correlated Linux file/text/shell evidence can establish this proof path: process/builtin operation → application-defined exit status or signal → shell pipeline/list/control-flow semantics → $? / PIPESTATUS → caller decision → target/workload verification | Use this observation only for the specific claim it actually supports. |
| Evidence limit | It does not independently prove behavior outside the captured host, shell/session, cwd/namespace, input, process, filesystem, locale, time or workload scope. Exit-code evidence proves only the producer's reported status under the caller's shell semantics; it does not independently prove side effects or end-to-end success. | Do not promote a local success signal into end-to-end or adjacent-dependency proof. |
| Next troubleshooting check | Continue from the first unproven boundary: capture the exact shell-delivered argv/streams or resolved object, verify the effective target state, change only the demonstrated cause, then repeat the original consumer/script/workload and adjacent regression checks. | Continue to the next unresolved boundary while preserving the original test condition. |
Exit Codes · operational reference
Compact Linux state, persistence, verification and boundary reference for fast lookup.
| Reference boundary | Exit Codes |
|---|---|
| Topic path | Shell & Bash → Exit Codes |
| Runtime state | Exit Codes are small integer process/shell statuses used to signal success or categories of failure to a parent shell; Bash also synthesizes statuses for builtins, pipelines, command-not-found and signal termination. |
| Verification chain | process/builtin operation → application-defined exit status or signal → shell pipeline/list/control-flow semantics → $? / PIPESTATUS → caller decision → target/workload verification Boundary chain: local authoritative state → adjacent dependency → effective/programmed state → original workload/request → return/regression → explicit Evidence boundary. Boundary chain: local authoritative state → adjacent dependency → effective/programmed state → original workload/request → return path or regression → explicit Evidence boundary. |
| Failure boundary | Exit-code evidence proves only the producer's reported status under the caller's shell semantics; it does not independently prove side effects or end-to-end success. Boundary chain: process/context → userspace configuration → kernel subsystem/resource → device/network/filesystem dependency → effective state → original workload. |
| Persistence rule | Prove which configuration source owns the state, then prove that the running consumer loaded or generated the intended state. |