Every process returns one number
When a program ends, it calls exit(3), or the lower-level _exit(2) system call, with a status number. exit(3) runs cleanup handlers and flushes the standard I/O buffers before it makes the system call, while _exit(2) ends the process at once. The parent process collects the number with wait4, which is the same collection step you saw in the system call lesson. Only the low eight bits survive. The status is therefore a value from 0 to 255.
You can see this with the shell. Bash exposes the last status as $?:
ls /nope; echo "status=$?"ls prints its error message to standard error, then exits with 2, and echo prints status=2. Read $? immediately after the command you care about. Any other command, including another echo, replaces it.
Conventions for the numbers
There is no universal meaning for each number. The program decides, and the man page or source documents the choice. The widely followed conventions are:
| Status | Meaning |
|---|---|
| 0 | Success |
| 1 | General failure; for grep and diff it means "no match" or "files differ" |
| 2 | Misuse or serious error; ls and grep use it for real errors such as an unreadable file |
| 126 | Found but not executable, such as a directory or a file without the execute bit |
| 127 | Command not found |
| 128+N | Killed by signal N; for example 130 is Ctrl-C (SIGINT, signal 2) and 143 is SIGTERM (15) |
Try the two failure cases the shell itself produces:
nosuchcommand; echo $?
/etc; echo $?The first line prints an error that the command is not found, then 127. The second prints that /etc is a directory and cannot be run, then 126. Both messages come from bash, not from a program.
true, false, and negation
true and false are commands that do nothing but exit with 0 and 1. They are useful for testing the shell itself:
false; echo $?
true; echo $?
! true; echo $?The output is 1, 0, and 1. The ! operator inverts a pipeline's status: it turns 0 into 1 and any nonzero status into 0. It is a shell keyword, not a command.
Chaining with && and ||
The && operator runs the right side only if the left side returned 0. The || operator runs the right side only if the left side returned nonzero. Both operators are evaluated left to right, with equal precedence:
false && echo yes || echo no
true && echo yes || echo noThe first line prints no: false fails, so echo yes is skipped, and the || branch runs. The second line prints yes, because true succeeds and the || branch is skipped. This left-to-right reading is why a && b || c is not the same as "a and then b or c" in every case. When b itself fails, c runs.
if runs a command, not a boolean
The if keyword does not evaluate a condition the way a programming language does. It runs a command and branches on its exit status. Any command works, and the most useful ones are quiet filters that report through their status. grep -q is the standard example: it prints nothing and exits 0 if the pattern matches, 1 if it does not, and 2 on an error.
if echo "hello world" | grep -q hello; then echo found; fiThe output is found. The pipeline's status is the status of its last command, which is grep here. [ and [[ work the same way: they are commands or keywords that return a status, and the if does not care how the status was produced.
Pipelines and their status
In a pipeline such as a | b | c, the status is that of the last command, c. The earlier commands can fail without changing the result. Bash keeps the status of each command in the array PIPESTATUS, which is overwritten by the next pipeline:
false | true; echo $?
false | true; echo ${PIPESTATUS[@]}The first line prints 0, because true was last. The second prints 1 0, the status of each stage. Each line reads the status right after its own pipeline. Any later command, including a separate echo $?, overwrites both $? and PIPESTATUS, so capture them immediately if you need them.
To make a pipeline fail when any stage fails, enable the pipefail option. It is off by default. Turn it on, run the check, then turn it off again so the change does not leak into later work:
set -o pipefail; false | true; echo $?; set +o pipefailThe output is 1. With pipefail, the pipeline's status is the rightmost nonzero status, or 0 if every stage succeeded. This option is common in scripts, and it is also a useful habit to know when a long pipe reports success after a stage has failed.
Signals and Ctrl-C
A process that dies from a signal has no exit call to give a status, so the shell reports 128 plus the signal number. Bash does not hide this. Send a process a SIGTERM from inside a shell to see it:
bash -c 'kill -TERM $$'; echo $?The output is 143, which is 128 plus 15. If you run sleep 100 at the prompt and press Ctrl-C, the status is 130, because Ctrl-C sends SIGINT, signal 2. To see it yourself, run sleep 100, press Ctrl-C, and then run echo $? at the next prompt. The same number appears for any command that you stop this way.
When a command fails with 2, read its message before you retry. Exit 2 usually means the tool was given something it could not use, while 1 often just means "no match" for a search tool. Treating both the same hides the difference.