Bash exit codes: 0 is success, and && and || use it
$? holds the exit status of the last command that ran; zero is success in shell tests, and non-zero is not success.
Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.
TE-37-A: zero means success in shell tests, non-zero means not-success; some tools use non-zero for a normal result
Setup for this run, inside a throwaway directory (mktemp -d):
printf 'a\n' > f.txtCommand:
true; echo true=$?; false; echo false=$?; grep -q z f.txt; echo grep_nomatch=$?; grep -q a f.txt && echo and_ran; grep -q z f.txt || echo or_ranRecorded output:
true=0
false=1
grep_nomatch=1
and_ran
or_ranTE-37-B: a shell function can return its own status, and $? reads it
No setup needed: the command below creates what it uses in the throwaway folder.
Command:
f() { return 3; }; f; echo rc=$?Recorded output:
rc=3Exit status: 0. This status and the output match what was written before the run.
TE-37-C: expected non-zero exit: a script that ends with exit 3 gives status 3 to its caller
No setup needed: the command below creates what it uses in the throwaway folder.
Command:
bash -c 'exit 3'; echo inner=$?; exit 3Recorded output:
inner=3Exit status: 3. This status and the output match what was written before the run.
Reading the recorded output
true=0 and false=1 show the exit statuses of the commands true and false. grep_nomatch=1 shows that grep -q exits with 1 when the text z is not in f.txt. The line and_ran was printed because grep -q a f.txt found a match and exited 0, so the command after && ran. The line or_ran was printed because grep -q z f.txt exited 1, so the command after || ran.
TE-37-B: the function returned 3, and $? read rc=3 right after the call. TE-37-C: bash -c 'exit 3' gave inner=3, and the outer command then ended with exit status 3 as well.
Limits
Some tools use a non-zero status for a normal result, such as no match or a difference.
Source
Evidence
TE-37-A: zero means success in shell tests, non-zero means not-success; some tools use non-zero for a normal result
- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-37-A | zero means success in shell tests, non-zero means not-success; some tools use non-zero for a normal result | 0 | PASS | 0c6377a24720 | | TE-37-B | a shell function can return its own status, and $? reads it | 0 | PASS | 84aad86f0bd3 | | TE-37-C | expected non-zero exit: a script that ends with exit 3 gives status 3 to its caller | 3 | PASS | 4fc9bcddd3a8 |
Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.
All full guides: All guides