Linux Command Reference ยท Concepts
Exit codes cheat sheet
A quick table of common Linux exit statuses and signal codes: 0, 1, 2, 126, 127, 128+N, 130, 137, 139, 143, plus tool-specific codes for grep, diff, curl and ssh.
An exit code (exit status) is the number a finished process hands back to its parent: 0 for success, anything from 1 to 255 for failure. The shell keeps the last one in $?. This page is the lookup table; for how to set and test statuses see Bash exit status.
Most codes are conventions, not laws, but a handful are fixed by the shell and the kernel. When a process is killed by a signal, the shell reports 128 + signal number, so a status above 128 usually means "it did not exit by itself". Knowing the table lets you diagnose a failed job from its number alone.
some_command echo "status $?" kill -l 9 # translate a signal number to a name
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
The table
Shell and kernel codes
| Code | Meaning |
|---|---|
| 0 | Success. |
| 1 | General error; the default failure of most tools and false. |
| 2 | Misuse of a builtin or an invalid option (some shells also use it for syntax errors). |
| 126 | Command found but not executable (permissions or wrong file type). |
| 127 | Command not found (also an unknown interpreter in a shebang). |
| 128 | Invalid argument to exit. |
| 128+N | Terminated by signal N. |
| 130 | 128+2, SIGINT: you pressed Ctrl+C. |
| 131 | 128+3, SIGQUIT: Ctrl+\, often with a core dump. |
| 137 | 128+9, SIGKILL: forced kill, frequently the out-of-memory killer or kill -9. |
| 139 | 128+11, SIGSEGV: segmentation fault. |
| 141 | 128+13, SIGPIPE: wrote to a closed pipe, e.g. yes | head. |
| 143 | 128+15, SIGTERM: polite termination request, e.g. kill PID, systemctl stop. |
| 255 | Exit status out of range, or a generic fatal error (also ssh connection failure). |
Common tool conventions
| Tool | Codes |
|---|---|
grep |
0 match found, 1 no match, 2 error. |
diff, cmp |
0 identical, 1 different, 2 trouble. |
test, [ |
0 true, 1 false, 2 bad expression. |
timeout |
124 the command timed out, 125 timeout itself failed, 126 and 127 as above; --preserve-status keeps the command's own. |
ssh |
The remote command's status, or 255 if the connection or authentication failed. |
curl |
6 host not resolved, 7 connection refused, 22 HTTP error with -f, 28 timeout. |
sudo |
The command's status; 1 if sudo itself refuses. |
tar |
0 ok, 1 files changed while archiving, 2 fatal. |
rsync |
23 partial transfer due to error, 24 files vanished, 30 timeout. |
make |
2 when any recipe failed. |
Errors and edge cases: statuses are 8 bits, so exit 256 becomes 0 and exit -1 becomes 255. Statuses above 255 can never be observed.
Usage notes
- Above 128 means a signal, so subtract 128 and look the number up with
kill -l. Conveniently,kill -l 137prints the name directly. - 137 in a container almost always means the process hit its memory limit and was killed; check
dmesgor the container runtime's OOM report. - Pipelines report the last stage; use
set -o pipefailor${PIPESTATUS[@]}to see the others: Strict mode. - Pick your own codes with care. In scripts use 0, 1, 2 and a small range such as 64-113 (the BSD
sysexits.hconvention starts at 64); avoid 126-255. - A service's exit code appears in
systemctl statusand decides restarts: Restart policies. $?is overwritten by every command, includingecho. Save it right away:rc=$?.- Test, do not print.
if grep -q pat file; then ...is more robust than comparing$?to numbers.