Every time you execute a command in a Linux terminal, a numeric exit code is produced, even if it’s not immediately visible. This code indicates whether the command succeeded or failed. In cases of failure, it provides a numerical value that can point to the nature of the issue. A code of 0 indicates success, and you will typically see the expected output from the command. If the command fails, you’ll likely encounter some form of error message, such as “permission denied” or “command not found.”
If you wish to reveal the numeric exit code, you can utilize the echo $? command, as demonstrated in the examples below.
Take note that the initial command executed successfully (exit code 0). Conversely, the subsequent commands did not execute successfully (exit codes of 1 and 2), each reflecting distinct issues indicated by their respective exit codes. The following commands yield additional exit codes of 126 and 127, stemming from different problems.
Specifically, an exit code of 126 indicates that a command attempted to execute but could not be performed. On the other hand, an exit code of 127 signifies that the command was not found. This could be the result of the file not existing or being located outside of your designated search path.
When executing commands in the command line, the accompanying error messages usually guide you on the next steps. However, scripts can present a slightly different scenario. The script illustrated below conducts a test to check whether the specified year falls within the acceptable range. If it does not, it generates both an error message and an exit code. The exit code holds little significance unless another process is monitoring the script’s successful execution.
Any exit code apart from 0 signals that the script (or a command executed in the command line) encountered an issue. To confirm that one script has successfully completed its execution in relation to another script, a straightforward if statement can be employed as follows:
It’s important to remember that the exit code at the end of a script is determined by the last command run. The script presented below would conclude with an exit code of 0 if the exit command was not present, as the if/then command does not trigger any errors.
Exit codes range from 0 to 255. If you were to issue an “exit 256” command in a script and subsequently check the exit status, you’d find it equals 0! Since the exit code occupies just a single byte, it essentially behaves as a modulo 256. Similarly, issuing “exit 258” will result in an exit code of 2.
There are various special exit codes that serve different purposes in scripting. For instance, exit code 2 signals a misuse of a shell builtin, while 127 indicates that a command was not found. Exit code 128 represents an invalid exit code, and numerous codes above 128 correlate with fatal errors due to signals, such as 130, which reflects a fatal error caused by a control C interruption. Exit code 1 is generally used as a generic identifier for various errors. If you plan to create specific exit codes for distinct issues in your scripts, it may be advisable to utilize numbers ranging from 3 to 125 or, as some proponents of effective return code practices suggest, between 64 and 113. By selecting a standardized set of codes tailored to your requirements, you can enhance the clarity and efficiency of your coding procedures.
Below is a summary of exit codes along with their corresponding error types:
Poorly crafted scripts might execute certain tasks for the user without verifying whether those tasks were carried out successfully. For instance, a script could output a series of messages indicating that installations are taking place, yet fail to check if the necessary packages are available or if the installations were carried out correctly. Some scripts might return a non-zero status code when errors occur, yet they disregard the status code when one script invokes another. Furthermore, I have encountered scripts that announce “successfully installed” messages, despite the reality that the installation process was far from successful.
Welcome to DediRock, your trusted partner in high-performance hosting solutions. At DediRock, we specialize in providing dedicated servers, VPS hosting, and cloud services tailored to meet the unique needs of businesses and individuals alike. Our mission is to deliver reliable, scalable, and secure hosting solutions that empower our clients to achieve their digital goals. With a commitment to exceptional customer support, cutting-edge technology, and robust infrastructure, DediRock stands out as a leader in the hosting industry. Join us and experience the difference that dedicated service and unwavering reliability can make for your online presence. Launch our website.