Bash Exit Command and Exit Codes

The bash “exit” command is a shell builtin that terminates the shell in which the script is running. The exit codes are numerical values returned to the shell when a command finishes running. These values are returned to the parent process, which can then use the value to determine the success or failure of the command. In a shell script, the exit command can be used to return an exit code to the shell when the script finishes running.

This write-up will discuss different approaches to using the exit command and exit codes in a Bash script with these supporting topics:

Let’s start with the examples.

Examples of Bash Exit Command in Linux

Every Linux command returns an exit code depending upon the execution result of the exit command. In case of successful execution, the exit code is “0”, but when the output shows an error, the output is between “1-255”, depending upon the type of error.

The commonly used exit command codes are as follows:

0Shows the exit code on successful execution.
1The exit code 2 is shown when the command output is no such file or directory.
2Exit status for invalid options.
126It shows the error when the server-required key is not available.
127This exit code represents a command not found error.
128Invalid argument error shows that 128 exit code.

Note: The bash script executed throughout this article is “exit.sh”.

Example 1: Exit Command to Quit the Terminal

The exit command quits the terminal, and to use it, just write the exit keyword. For instance, the below bash script will execute the command “pwd” before the exit command. The command after “exit” will not be executed:

#!/bin/bash
pwd

exit   echo "After exit Command Line"

To execute the bash script “exit.sh”, just write the bash script name with the “bash” command as shown below:

$ bash exit.sh

The output shows that it quits the terminal with the exit command, and the echo command after the exit command is not executed.

Example 2: Bash Exit Code After Successful Execution

When the terminal is exited, a specific exit code is generated according to the output, which can be seen with the “echo$?” command. The “echo $?” only captures the exit code and does not exit the terminal. For instance, to check the exit when the execution is completed, which code is generated, use the below bash code:

#!/bin/bash
echo "Hello World!"

echo $?

Execute the bash script code by running this command:

$ bash exit.sh

The echo command is executed successfully and the exit shows it is “0”, which verifies that the command execution is successful.

Note: If a command is after the “$?” command, it will execute.

Example 3: Bash Status After Unsuccessful Execution

There are several errors in Linux, and every error is associated with a specific exit code. For instance, we want to check the exit code for the unsuccessful execution of “no such file or directory” with the cat command:

#!/bin/bash
cat sample.txt

echo $?

Execute the bash script code by running this command:

$ bash exit.sh

The exit status is 1 for the “no such file or directory” as verified from the output.

Example 4: Bash Exit Code for Invalid Option Error

If you have incorrectly written any option that is not available in that command, the error “invalid option” is shown, and its exit code can be verified with the below code:

#!/bin/bash
ls -e

echo $?

Execute the bash script code by running this command:

$ bash exit.sh

The exit code for the invalid option error is 2.

Example 5: Bash Exit Code After Command not Found

We can capture the error code for the error “command not found” and execute the successful command after capturing the exit code with the below code:

#!/bin/bash
hostnam

echo $?

echo "Hello!"

Execute the bash script code by running this command:

$ bash exit.sh

The output shows the 127 error code for the command not found, and it also executes the error after that.

Example 6: Exit the Terminal for Successful Execution

With the exit command, you can terminate the terminal using its particular exit code. For instance, to exit the terminal for exit code “0”, which is for successful execution, use the below:

#!/bin/bash

echo "Above test line"

exit 0

echo "Below test line"

Execute the bash script code by running this command:

$ bash exit.sh

The output shows that the first echo command executed successfully and the bash script is exited. The second command is not executed that is after the exit command.

Example 8: If Else Loop to Check Bash Exit Status

We can check if the execution is successful with the if-else conditional loop in bash. For instance, the below code takes the user input with the read command and executes that command. If the user input command is executed successfully, it shows the message “Command Executed Successfully” otherwise, it will show the “Command Not Executed Successfully” status:

#!/bin/bash
echo "Hello World!"
if [ $? -eq 0 ]
then
  echo "Command Executed Successfully"
else
  echo "Command Not Executed Successfully" >&2
fi

Run the bash script code as follows:

$ bash test.sh

The echo command ran perfectly and showed the status “Command Executed Successfully”.

Conclusion

The bash exit command is used to quit the terminal or script and shows a specific exit code based on the command execution. The exit command can be combined conditionally with the exit code to quit the terminal based on the exit status. Moreover, the exit code shows whether the command is successful or not.