Pcornel
Automation · Scripting

Exit Codes

Cheat sheet · https://pcornel.dk/topic.php?topic=5221

Back to the Topic

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

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

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=5221

Confirmation

Confirm action

Continue?