Exit Codes
Cheat sheet · https://pcornel.dk/topic.php?topic=5221
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.
Remember
- 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.
First checks
echo $?— last exit code (Bash)$LASTEXITCODE— PowerShell native command exit- Documented exit codes — contract
Verify in this order
script → exit code → caller decision
Where it usually breaks
An exit code proves only the process-level result according to the script's contract; it does not independently prove target state or identify root cause.
Script/input → command/module binding → local/remoting session → request accepted → command result → target object read-back → effective runtime state → workload. Preserve at least three independent checks: prerequisite/interface or identity/transport state; the owning control/service/policy state; then effective data-plane, backend or workload state. A healthy upstream boundary does not prove the downstream consumer or return path.
Exit Codes scripting boundary map
| Boundary | Healthy evidence | Suspicious evidence |
|---|---|---|
| Input / runtime | Effective inputs, runtime, working context and dependency versions are explicit | Hidden defaults, stale environment, wrong interpreter/path or unvalidated input |
| Logic / execution | Branch/function/module decisions and external actions expose per-operation results | Swallowed failures, shell reinterpretation, blind retries or last-result-wins behavior |
| Result / proof | Exit/exception/log evidence is correlated with independent target read-back | Process success or printed output treated as target success |
| Recovery / troubleshooting | First failed boundary is preserved, changed minimally and retested | Random edits, catch-all suppression or evidence destroyed before diagnosis |
Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=5221