What Will We Cover?
In this guide, we will see an overview of the exit command in Linux with some use cases.
You can also use the exit command to specify the exit status of the shell. The exit status is a numeric value that is returned to the parent process when the shell terminates.
The exit status can be used to indicate the success or failure of the command. For example, a shell script might use the exit status to determine whether to perform certain actions based on the success or failure of a command.
To specify the exit status, you can use the exit command followed by a number. For example, exit 0 indicates a successful exit, while exit 1 indicates a failure.
The exit command accepts a single, optional argument which becomes the script’s exit status.
When no argument is passed, the exit status defaults to the exit status of the last command that is executed. In order to see the exit status, use the echo “$?” command.
The exit command is crucial to indicate that a severe error has occurred since the calling processes typically consider the exit codes other than zero as failure cases. If you need a general error trap in your scripts, try using the exit code snippet.
Let’s see some examples of using the exit code in a script and on a terminal:
Example 1: Using the Exit Command with File Expression
Let’s first take an example of a script:
# test-file: Evaluate the status of a file
if [ -e "$FILE" ]; then # Check the existence of file.
if [ -f "$FILE" ]; then # Check if it is a regular file.
echo "$FILE is a regular file."
if [ -b "$FILE" ]; then # Checks if it is a block file
echo "$FILE is a block device"
echo "$FILE is neither a regular nor a block file or may not exist"
The script performs an evaluation on the file that is allocated to the constant “FILE” and outputs the results as it runs.
First, it checks if it is a regular file or a block file. If the condition is not satisfied, it prints the message in the else block.
The first exit command sets the exit status of the script in case of failure (exit value as 1). The second exit command is a formal expression. This means that when the script completes its execution to the end, it has the exit status of the last command which, in this case, is the exit command. This last exit command will result in success (exit value as 0):
Example 2: Using True and False Commands with the Exit Command
Additionally, entering a simple true or false argument causes the terminal to quit with 0 status code and shows 0 as true. If we instead provide a false statement, the exit status will be 1 since false exits with code 1. Let’s see this in a practical manner. First, issue the true command and check the exit status:
$ echo $?
Now, issue the false command and again check the exit status:
$ echo $?
Example 3: Interrupting a Script with Ctrl+C
When a user hits Control-C, a special signal known as SIGINT is sent. This interrupts the script or running program, causing it to exit with the code 130. Let’s take an example: suppose you started searching a file with the find command:
The search is still going on. Then, you suddenly interrupted it with “Ctrl+C”. This causes the command to stop with the exit code 130.
Example 4: The Shell Variable SHLVL and the Exit Command
With the shell-specific variable, SHLVL, you can see how many levels deep in the Bash shell you are. Each new shell session that is opened in a terminal increases the session count, starting at 1 for the initial session. If SHLVL is > 1, the exit command closes the current session. In the same way, if the SHLVL is = 1, the exit command logs you out of the system.
To simply put it, when you type “exit” at the command prompt and press Enter, the shell closes (close the current login session) and you will be returned to the shell or command prompt from which you launched it.
We have seen the different use cases of the exit command and the interpretation of the exit status in each case. There are many different exit code numbers for different purposes. Note that these exit codes are Bash specific. If you tend to use other shells such as C-shell or tcsh, the results may differ.