This article explores exit codes in shell scripts, indicating command success or failure on Linux systems.
In this article, we explore the concept of exit codes in shell scripts and how they indicate whether a command executed successfully or encountered an error on Linux systems.When you run a command, it either executes successfully or fails. For instance, listing the contents of the current directory with the command below executes successfully and returns an exit status of zero:
Copy
Ask AI
$ ls/home/root/tmp
Conversely, if you run a command that does not exist, an error is displayed and a non-zero exit code is returned:
Copy
Ask AI
$ lssFailed: command not found
When a command runs successfully, it returns an exit status of 0; when it fails, it returns a non-zero value. These exit codes are not shown in the command output but are stored in the built-in variable ”$?”.To view the exit code immediately after executing a command, use:
Copy
Ask AI
$ ls/home/root/tmp$ echo $?0$ lssFailed: command not found$ echo $?127
It is considered best practice to use exit codes in your scripts to communicate the overall status to the caller or user.
Consider a scenario where you are launching a rocket mission. For a successful launch, the script should return an exit status of 0, and for any failure, it should explicitly return a non-zero value (commonly 1).Below is a sample script that starts a rocket launch mission. In this naive version, even if the launch fails, the script returns an exit code of 0 because it only prints a failure message without setting a non-zero index:
However, checking the exit code reveals that it remains 0:
Copy
Ask AI
$ echo $?0
To address this issue, update the script so that it returns a non-zero exit code (typically 1) when the launch fails. Here is the improved version of the script:
Now, if the rocket launch fails, the script will correctly exit with a status code of 1. This can be verified as follows:
Copy
Ask AI
$ create-and-launch-rocketfailed
Copy
Ask AI
$ echo $?1
Always ensure that your scripts return an appropriate exit code. Explicitly returning a non-zero code for failure conditions facilitates better integration with other systems and scripts.