Exit status is a number
When a command finishes, it returns a status between 0 and 255. The shell stores the status of the most recent command in the special parameter $?. Zero means success. Any nonzero value means the command failed or was stopped. Different numbers can carry different meanings for a particular program. The grep program uses 1 for no match and 2 for an error, while diff uses 1 for differences. Read the manual page of a program for its own values.
grep -q root /etc/passwd
echo $?Read $? right after the command you care about, because the next command overwrites it. Store the value in a variable if you need it later. For a pipeline, $? holds the status of the last command in the pipeline, and the array PIPESTATUS keeps the status of every stage.
Setting the status with exit
A script ends with exit N, and N becomes the status of the script. Without an argument, exit returns the status of the last command that ran. A function ends with return N, which sets the status of the call and does not stop the script. Values are reduced modulo 256, so exit 256 returns 0.
The status of a script is what the caller sees in $?, and what && and || test. A script that prints an error and exits with a nonzero status is much more useful than one that prints an error and then succeeds. The caller can react to the status without parsing text, and that is the design the rest of the shell relies on.
The standard values
A few values have conventional meanings. Zero means success. One means a general failure. Two is the usual status for misuse of a command, such as a bad option, and many scripts use it for usage errors. The value 126 means the file was found but could not be executed. The value 127 means the command name was not found. A value above 128 means the process was ended by a signal, and 128 plus N gives the number of the signal N. Ctrl-C sends SIGINT, which is signal 2, so an interrupted command reports 130.
A status of zero is the only value that counts as success in an if test or an && list. Any other value, including values that a program uses for warnings, counts as failure to the shell. If a program reports a warning with a nonzero status that you want to treat as success, you must handle that case explicitly, for example with a || branch that checks the specific value.
These values come from the shell and the kernel, not from the program. A script that wants a clear status should choose from the same conventions, so that callers can test for the situations they care about.
&& and || run based on status
The operator && runs the right side only when the left side succeeded. The operator || runs the right side only when the left side failed. Both are short-circuit operators, so the shell evaluates the right side only when it is needed. That is what makes them useful as guards.
mkdir -p backup && cp notes.txt backup/
test -d backup || echo "no backup directory"The first line copies the file only if the directory exists or was created. The second line reports the problem only when the directory is missing. These operators express a small if statement in one line, which is fine for simple cases. For anything with several steps, use if, because a long chain becomes hard to read.
The trap in a && b || c
A chain such as a && b || c looks like an if-then-else, and it nearly is. The command c runs when a fails, but it also runs when a succeeds and b fails, because then the chain ends with a nonzero status. If b can fail, the else branch can run after a success. The example below shows the problem:
true && false || echo runsThe command prints runs, even though the first command succeeded. The middle command failed, so the || branch ran. When the middle command can fail, write the logic as an if statement, where each branch is explicit.
Negation and pipelines
The ! prefix inverts the status of a pipeline. So ! grep -q err log is true when the file has no match. The errexit option, set with set -e, does not stop a script for a command that is part of an if test, an && or || list, or a negated pipeline. That is why a script with set -e can keep running after a failed test.
A pipeline reports the status of its last command by default. A failing producer can hide behind a successful consumer, so false | cat returns 0. The command set -o pipefail makes the pipeline report the rightmost nonzero status among its stages, which exposes the failure.
Hands-on: check statuses
Run the lines below one at a time and compare each result with your prediction.
false; echo $?
true && false || echo runs
false | cat; echo $?Each status in this unit has a practical use. A script that checks a precondition should exit with a nonzero status when the precondition fails, so that the caller can stop before doing damage. A script that reports a usage error should use 2. A script that finishes its work should exit with 0 explicitly, so that a stray failing command earlier in the file does not leak out as its result.
The output should be 1, then runs, then 0. Next, run set -o pipefail, repeat the third line, and see that the status becomes 1. Run set +o pipefail afterward to restore the default for the rest of your session.