Each time you execute a command in a Linux terminal, an exit code, represented as a number, is created – often without any visible output. This code indicates whether the command was successful or not. In cases of failure, the exit code provides a hint about the issue encountered. An exit code of 0 signifies that everything executed properly, and you’ll see the expected output from your command. Conversely, any other exit code suggests an error, which might be something like “permission denied” or “command not found.”
To check the numeric exit code, you can use the command echo $?
as demonstrated in the examples below.
In the examples, the first command executed successfully (exit code of 0). The subsequent commands encountered errors, resulting in exit codes of 1 and 2, each indicating different issues. Other commands produced exit codes of 126 and 127, indicating entirely different problems.
For instance, an exit code of 126 signifies that the command was not executed, while an exit code of 127 indicates that the command is not found. This may occur if the specified file is missing or if its path is not included in your search settings.
When using the command line, error messages usually provide clear guidance on the next steps to take. However, scripts can be a bit more complicated. For instance, the script presented below tests whether a given year falls within an acceptable range. If the year is outside this range, it generates an error message along with an exit code. This exit code is not particularly useful unless another process is monitoring the success of the script.
An exit code that is anything other than 0 signals that a script or command has encountered a failure. To confirm that one script has completed successfully from another, you can implement a straightforward if statement like the following:
It’s important to remember that the exit code at the conclusion of a script is determined by the last executed command. In the example below, the script would conclude with an exit code of 0 if the exit command were omitted, since the if/then statement does not trigger any errors.
Exit codes have a range from 0 to 255. If you use the command “exit 256” in a script and check the exit status afterward, you will find it to be 0! This is because the exit code is limited to a single byte, effectively making it operate under modulo 256. Similarly, using “exit 258” would yield an exit code of 2.
There are various special exit codes to be aware of, such as 2, which indicates the misuse of a shell builtin, and 127, which signifies that a command could not be found. Exit code 128 represents an invalid exit code, while codes greater than 128 typically correspond to fatal errors (with 130 specifically denoting a control C fatal error). Conversely, exit code 1 serves as a general error code for numerous issues. If you aim to define your own exit codes for different problems in your scripts, a good practice would be to utilize codes ranging from 3 to 125, or, as some contributors suggest, between 64 and 113. This way, you’ll have an ample selection of return codes at your disposal, allowing for a more standardized coding approach that meets your specific needs.
Below is a summary of exit codes along with the error types they signify:
Scripts that are poorly designed may execute actions on behalf of a user without properly verifying if those actions succeeded. For instance, a script might display a series of “installing …” prompts without confirming that the packages intended for installation are indeed available or that the installations were successfully completed. Some scripts may exit with a non-zero status whenever an issue arises, yet they may neglect to address the resulting status code when one script invokes another. It is not uncommon to encounter scripts that declare “successfully installed” messages, even though the software installation was far from successful.