Pcornel
Linux · Shell & Bash

Exit Codes

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

Back to the Topic

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.

Remember

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

First 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

Commands

systemctl is-active
ping -c
test

Verify in this order

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

Where it usually breaks

First mismatch in persistent intent → provider/parser/generator → kernel/userspace runtime → dependency/security context → logs/counters → consumer-visible read-back; preserve identity/native diagnostic/pre-state before mutation.

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

Confirmation

Confirm action

Continue?