Pcornel
Browse topics
Options
Recent
Favorites
Linux · Technical reference

Exit Codes

Direct answer

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.

When to use this

Use it to make scripts react to failures, or to understand why a script continued after an error.

First 30 seconds

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.

Operational verification

command ends → exit status set → $? updated → &&, ||, if or set -e decide what happens next

Evidence · next step

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
Platform / version applicability
linux
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

Platformlinux
Scope / prerequisiteUse it to make scripts react to failures, or to understand why a script continued after an error.
What next?3 suggested next steps
Evidence1
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

5 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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 references

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.

observation · 1
Key points

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").

Key checks
  • bash -x script.sh — trace each expanded command
  • bash -n script.sh — syntax check only
  • shellcheck script.sh — common bugs and quoting problems
  • echo $? — 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

CodeMeaning
0Success
1General error
2Misuse of a builtin; many tools use it for usage errors
126Found but not executable (permissions, wrong format)
127Command not found (PATH, a typo)
130Killed by SIGINT (Ctrl-C): 128 + 2
137Killed by SIGKILL: 128 + 9 (often the OOM killer, or a container limit)
143Killed by SIGTERM: 128 + 15
255ssh: the connection itself failed

Tools that use exit codes meaningfully

ToolCodes
grep0 = match, 1 = no match, 2 = error
diff0 = same, 1 = different, 2 = trouble
systemctl is-active0 = active, non-zero otherwise
ping -c0 = replies, 1 = no reply, 2 = error
test / [ ]0 = true, 1 = false

Using them

PatternExample
Branch on successif grep -q ERROR app.log; then ...; fi
Run only on successmake && make install
Fallbackcurl -fsS url || echo "fetch failed" >&2
Save before it's overwrittencmd; rc=$?
End a script with a codeexit 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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

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:

CodeMeaning
1General error
2Misuse of builtin / bad usage
126Found but not executable
127Command not found
128+NKilled 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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

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
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor doclinux current

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%
Technical changelog & impact

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.

Related reference impactReview these references after material technical changes when their shared boundary is affected.
Terminology layer

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.

Aliases that resolve here
bash exit codeslinux exit status$? bashPIPESTATUS exit code
Embedded reference data

Quick lookup tables

Exit Codes operational reference

Topic-specific lookup boundaries, proof limits and next-step Evidence for Exit Codes.

Lookup boundaryTopic-specific referenceOperational use
Scope / effective stateExit 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 verificationPin 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 chainprocess/builtin operation → application-defined exit status or signal → shell pipeline/list/control-flow semantics → $? / PIPESTATUS → caller decision → target/workload verificationTrace requested/configured state through effective state to the observed workload result.
Failure boundaryExit-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 evidenceCorrelated 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 verificationUse this observation only for the specific claim it actually supports.
Evidence limitIt 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 checkContinue 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 boundaryExit Codes
Topic pathShell & Bash → Exit Codes
Runtime stateExit 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 chainprocess/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 boundaryExit-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 ruleProve which configuration source owns the state, then prove that the running consumer loaded or generated the intended state.
Confirmation

Confirm action

Continue?