Pcornel
Automation · Bash Automation

Exit Codes

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

Back to the Topic

Answer

Exit codes are the number a command returns: 0 for success and 1–255 for failure, available in $?. Scripts should exit non-zero on errors so callers and schedulers detect failures.

Remember

  • 0 is success; non-zero is failure.
  • $? holds the last exit code.
  • Exit non-zero on errors.

First checks

  • echo $? — last exit code
  • set -e — exit on error
  • exit 3 — explicit code

Verify in this order

command → exit code → decision

Where it usually breaks

Input/desired state → request accepted → job/task terminal status → target object read-back → effective runtime state → original workload → regression/rollback readiness.

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

Confirmation

Confirm action

Continue?