What is the Meaning of exit 0, exit 1, and exit 2 in a Bash Script?

The “bash” script contains a sequence of commands that perform the various tasks in the system. To check whether all the commands defined in the shell script are executed successfully or not, bash uses the “exit” utility having values “0”, “1”, “2”, and so on.

The “exit 0”, “exit 1”, and “exit 2” commands return the exit status of the bash script and are stored in the “$(shell)” variable. The “$” variable can be easily accessed through the built-in Linux “echo” command.

This guide illustrates the meaning of “exit 0”, “exit 1”, and “exit 2,” along with the practical implementation in the bash script.

What is the Meaning of “exit 0” in Bash Script?

The “exit 0” denotes that the command is successfully executed, defined in the bash script. Follow the below example that illustrates how it works in a bash script.

Example:

Create/open a “Code.sh” bash script in the “nano” text editor containing “exit 0” at a specified place as shown below: 

$ nano Code.sh
#!/bin/bash
read -p "Enter a number:" x
if [ $x -eq 10 ]
then
echo "The entered number is one digit"
exit 0
else
echo "The entered number is two digit"
fi

The above block has following lines of code:

  • The “#!/bin/bash” denotes the “Bash Shebang” that executes the script in “bash” shell.
  • The “read” statement stores the variable “x” as input from the user.
  • Finally, the “if else fi” loop sets the condition that if the integer value is equal to 10, then the first “echo” command will execute, and the “exit 0” will not perform further commands execution. 
  • If the condition fails, the “echo” command of the “else” statement will be executed.

Save(Ctrl+S) and exit(Ctrl+X) the script file.

Execute the Code

Execute the “Code.sh” script and enter the number “98”:

./Code.sh

In this way, the “echo” statement of the “else” part is executed and displays the result without a sudden exit:

What is the Meaning of “exit 1” in Bash Script?

The “exit 1” (non-zero status) identifies an error in the shell script. It usually occurs when an incorrect argument is passed to the command. The following example illustrates its practical implementation.

Example:

A new “my_script.sh” script is created having the following content:

$ nano my_script.sh
#!/bin/bash
mkdir Pictures
echo $?

The “mkdir (make directory)” command will create a “Pictures” directory if it does not exist, and the “echo” command will print out the shell exit status:

Execute the Script

Now run the script to check its output:

./my_script.sh

The “exit” status of “my_script.sh” is “1”, which shows an error, i.e., the “Pictures” file already exists in the system.

Tip: Using the “exit 1” to Exit a Shell Script 

The “exit 1” command is also beneficial with the “||(OR)” operator to exit the script if any of its parts fail. Following “sample.sh” script shows its practical implementation.

Create/open a new “sample.sh” script in the “nano” editor having the following content:

$ nano script.sh

The “exit 1” terminates the script instantly if the “ls” command does not list down the “Extra” directory:

Output

Run the “script.sh” script and check its output:

$ ./script.sh

The “script.sh” script is exited instead of the incorrect syntax of the “echo” command.

What is the Meaning of “exit 2” in Bash Script?

The “exit 2”  is another non-zero value that also specifies the error occurrence in the shell script. It generally occurs when the script cannot find the targeted file. 

Example:

The script “New.sh” is present in the “home” directory that is opened in the “nano” editor:

$ nano New.sh
#!/bin/bash
ls Sample
echo $?

In the above script, the “ls” command lists the “Sample” directory content if it exists:

Save and exit the script file.

Execute the Script

To execute the script, specify the filename as “New.sh” in the following command:

./New.sh

The output displays the exit status of the script, “2”, which represents that there is no “Sample” file or directory.

Note: To explore more options for exit commands and code, follow our article “Bash Exit Command and Exit Codes”.

Conclusion

In the bash script, “exit 0”, “exit 1”, and the “exit 2” are used to return the exit status of the shell script. The “exit 0” returns the successful execution of all commands in the script. The “exit 1” is due to incorrect syntax, and the “exit 2” is if the script can not find the targeted file.

This guide has illustrated the meaning of “exit 0”, “exit 1,” and “exit 2,” along with their practical implementation in a bash script.