Exit codes and errors
8 minWhat a status means
Zero is success, and everything else is a number worth reading.
Every command leaves a number behind. Zero means it worked; anything else means it did not, and the particular number often says how.
$? holds the last one — and only the last one, so it is overwritten by the very next command, including by the echo you use to look at it.
Try this
The last line prints 0: echo succeeded. $? has to be captured before anything else runs.
A few numbers are conventional. 1 is an ordinary failure; 2 usually means the command line was wrong; 127 means the command was not found at all; and 128 plus a signal number means something killed it.
grep is the classic case: 0 matched, 1 did not match, 2 something went wrong. A script that treats 1 and 2 the same cannot tell 'no results' from 'no such file'.
Try this
Files
| Mode | Links | Size | Name |
|---|---|---|---|
| -rw-r--r-- | 1 | 63 | agenda.txt |
| -rw-r--r-- | 1 | 87 | receipt.txt |
| -rw-r--r-- | 1 | 122 | roster.md |