On Unix and Linux systems, every command ends with an exit status (also known as return status or exit code). Exit status is an integer value ranging from 0 to 255.
By default, a command that ends "successfully" has an exit status of zero, 0.
A command that ends with a "failure" has a non-zero (1 - 255) exit status.
|Note: Success and failure, with respect to exit status is relative. By default if a command does what it's expected to do, on exit it sets a zero, 0, exit status. E.g. If the directory /var/log/apt exists, the command ls /var/log/apt will end successfully and result in an exit status of 0. However if the argument, in this case a directory, is not accessible the ls command will "fail" and leave a non-zero exit status:
By convention, success results in an exit status of zero, however commands are generally free to decide what non-zero integer, between 1 and 255 to use. In the above example, ls uses an exit status of 2 to reflect that a directory is not accessible. And docker chooses an exit status of 125 to reflect that the image is not accessible from the repository.
Commands are free to choose which value to use to reflect success or failure. However there are some reserved value that have special meaning, defined here: http://www.tldp.org/LDP/abs/html/exitcodes.html
A command writes its exit status into the ? shell variable, accessible via $?. This variable can hold one value at a time, as such it is overwritten when the next command exits.
To read the previous command's exit status, use the command, echo $?.
|0||the exit status of a command on success|
|1 - 255||the exit status of a command on failure|
|?||holds the exit status of the last command executed|
|$?||reads the exit status of the last command executed|
- Reserved Exit Status Values: http://www.tldp.org/LDP/abs/html/exitcodes.html
|Licensed under a Creative Commons Attribution 4.0 International License.|