Explaining Exit Command and Exit Status in Bash

The aim of this page📝 is to describe exit and exit statuses in bash, in the form of my footnotes under the wonderful Advanced Bash-Scripting Guide > Exit and Exit Status

The exit command does 2 things

The <n> returned by the exit command is an exit status

$? reads the exit status of the last executed command

124: if COMMAND times out, and −−preserve−status is not specified

8 exit codes have reserved meanings and are general for all commands

— from Advanced Bash-Scripting Guide > Exit Codes With Special Meanings

In general, error codes are for bash — modern languages support exceptions and they should be used instead!

bash does not abort the running execution in case something detects an error state (unless you set the -e flag). Programming languages that offer try/catch do this to inhibit a "bailing out" because of this special situation (hence typically called "exception"). In the bash, instead, only the command in question will exit with an exit code greater than 0, indicating that error state. You can check for that of course, but since there is no automatic bailing out of anything, a try/catch does not make sense. It is just lacking that context

LINKS

--

--

Infrastructure Support Engineer/Technical Writer (snowplow.io) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavol Kutaj

Infrastructure Support Engineer/Technical Writer (snowplow.io) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com