Pcornel
Browse topics
Options
Recent
Favorites
Automation · Technical reference

Exit Codes · Scripting

Direct answer

Exit Codes provide a compact process-level result contract to callers and schedulers. Reliable scripts map meaningful success and failure classes consistently while preserving detailed diagnostics elsewhere.

When to use this

Use it when configuring, checking or troubleshooting Exit Codes in Scripting.

First 30 seconds

1. Exit 0 = success; non-zero = failure.

2. Define meaningful codes.

3. Propagate failures (set -e, pipefail).

4. Check native command codes in PowerShell.

5. Callers rely on them.

Operational verification

script → exit code → caller decision

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
general-purpose scripting automation
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.

Platformgeneral-purpose scripting automation · Scripting owns generic process result semantics; schedulers/orchestrators define how those codes affect workflow state and target Domains define outcome acceptance.
Scope / prerequisiteUse it when configuring, checking or troubleshooting Exit Codes in Scripting.
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.

4 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

operation result → classify terminal state → preserve first/required failure → map to exit code → caller/scheduler interpretation → correlated diagnostics

Healthy evidence: success and failure codes are documented, child failures propagate, partial failure cannot become zero and diagnostics correlate to the exit status.

Step 2 Failure modes and next checks Narrow the fault

Suspicious evidence: always-zero scripts, status overwritten by cleanup, inconsistent code meanings, non-zero ignored by callers or logs that disagree with the returned code.

Step 3 Common mistakes Narrow the fault
  • Ignoring exit codes.
Step 4 Where it fails Narrow the fault

Where Exit Codes usually goes wrong: process exit-status contract, signal/termination distinction, pipeline propagation and semantic mapping.

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
  • Exit code 0 ≠ business outcome success; the script may not have verified the target or may define success too weakly.
  • Non-zero exit ≠ root cause; it signals failure class, while logs/exception/command evidence explain why.
  • Last command exit code ≠ whole script status; later commands can overwrite prior failure state.
  • Printed error ≠ failed process; a script can emit an error message and still return zero unless explicitly handled.
Key checks
  • echo $? — last exit code (Bash)
  • $LASTEXITCODE — PowerShell native command exit
  • Documented exit codes — contract
ReferenceWhat it is1 section · depth on demand

Core model

Define which terminal conditions map to zero and non-zero codes, propagate child-process failures deliberately, avoid overwriting an earlier failure with later success and pair the code with structured context for diagnosis.

ReferenceImportant terms & boundaries2 sections · depth on demand

What this shows and what it does not

An exit code shows only the process-level result according to the script's contract, but it does not show target state or identify root cause.

In depth

Exit codes are the number a program returns when it ends: 0 means success, anything else means failure. Schedulers, CI pipelines and other scripts rely on them to decide whether to continue, so scripts must return honest exit codes.

CodeCommon meaning
0Success
1General error
2Misuse (bad arguments) in many tools
126/127Not executable / command not found (shell)
128+nKilled by signal n (e.g. 137 = SIGKILL)
set -euo pipefail
backup.sh || { echo "backup failed with $?" >&2; exit 1; }
& robocopy src dst /E; if ($LASTEXITCODE -ge 8) { exit 1 }   # robocopy: <8 is success
Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.
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 docgeneral-purpose scripting automation current

Authoritative/upstream documentation reviewed for bounded claims in Scripting > Exit Codes. Documentation verification only; this is not observed lab or production Evidence.

Basis: https://docs.python.org/3/ · confidence 94%
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
script exit codesexit codesreturn codesprocess exit status
Embedded reference data

Quick lookup tables

Exit Codes scripting boundary map

Fast input, runtime, execution, result and verification boundaries for Exit Codes.

BoundaryHealthy evidenceSuspicious evidence
Input / runtimeEffective inputs, runtime, working context and dependency versions are explicitHidden defaults, stale environment, wrong interpreter/path or unvalidated input
Logic / executionBranch/function/module decisions and external actions expose per-operation resultsSwallowed failures, shell reinterpretation, blind retries or last-result-wins behavior
Result / proofExit/exception/log evidence is correlated with independent target read-backProcess success or printed output treated as target success
Recovery / troubleshootingFirst failed boundary is preserved, changed minimally and retestedRandom edits, catch-all suppression or evidence destroyed before diagnosis
Depth when you need it

Deep reference

Healthy vs suspicious evidence

Output interpretation

For Exit Codes, rely on structured fields and stable IDs (process exit-status contract, signal/termination distinction, pipeline propagation and semantic mapping) rather than text formatted for display.

Confirmation

Confirm action

Continue?