Linux bash shell provide simple but useful programming environment. We can write simple applications, scripts which will run commands, redirect input and outputs and create, stop process. After the execution of the script or function we may need to provide some information to script caller. exit
keyword is used to end given script with useful information. In this tutorial we will examine different use cases with exit
and send exit status to the program or script caller.
Exit With Status Of Last Command
We will start by providing the status of the last command as exit status. We generally run commands in scripts or applications. Every command executed in this scripts or applications will finish execution with a status. The last command exit status can be provided at the end of the script in 3 different ways.
- Simply doing nothing will provide the last command exit status as the script exit status. In the following example the
ls
command exit status will be also the exit status of the script.
#!/bin/bash ls /home
- Or we can explicitly use the
exit
keyword in order to send the staus of thels
command like below.
#!/bin/bash ls /home exit
- Or the most explicit version is using
exit $?
which will return the last executed command status with$?
by providingexit
.
#!/bin/bash ls /home exit $?
Print Last Command Exit Status
As we learned in previous part we can use $?
in order to provide the last command exit status. This is actually a bash feautre which can be used in different ways apart from scripts. Here we will run mkdir
command with a unsuccessful operation to create a folder in /root/test
which will give Permission denied error. Then we will print the exit status of this mkdir
command $?
$ mkdir /root/test $ echo $?

As we can see this will print 1
which means there is an error. We will learn exit status codes in the following parts.
Explicitly Provide Exit Status
While exexuting scripts and applications we can provide the exit code explicitly. We just provide the code we want to provide to the exit
keyword. Keep in mind that the exit
will exit the current running script,application or process. In this example we will provide the exit status as 17
.
#!/bin/bash ls /home exit 17
Success Exit Status
There are different exit status codes and meanings. But there are some general rules. Success exit status code is 0
which will accepted by the Linux community. In the following example we will provide successful exit status from given script.
#!/bin/bash ls /home exit 0
General Errors Exit Status
As stated previously there different type of codes. We can also create our own codes and their meanings. Here is most generic and accepted exit status codes.
0
exit code is used to state command or script execution is completed succesfully.1
exit code is used to state that there are general errors likedivide by zero
etc.2
exit code is used to state there is a misuse of the shell builtin.
We can also create our own exit status code and interpret it. For example we can use 17
as successful completion but there are some minor warnings.
exit 17
Set Exit Status Into A Variable
While working with exit status we may need to set the exit status of given script, application or command into a variable. This is very simple case where we will use $?
and variable assignment for this.
$ mkdir /root/test $ last_exit_status=$? $ echo $last_exit_status

As we can see the exit status is saved into variable named last_exit_status
and get the exit value 1
1 thought on “Linux Bash exit and Exit Codes”