Exit Statuses and Flow Control
In programming languages, functions and methods generally return a value. Unix processes are the same. At the point that they exit, they set an exit status that tells you something useful about what happened when the process ran.
This exit status is always a number between 0 and 255. Convention dictates that 0 indicates success and that anything greater than 0 indicates some sort of failure. Individual programs are allowed to use whichever non-zero codes they like for whichever purpose, though it’s common to find programs that simply set a status of 0 for success and 1 for all failure states.
After running a command, we can check the value of the exit status that it set by inspecting the $? variable:
| | user@computer:~ ... |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access