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
lscommand exit status will be also the exit status of the script.
#!/bin/bash ls /home
- Or we can explicitly use the
exitkeyword in order to send the staus of the
lscommand like below.
#!/bin/bash ls /home exit
- Or the most explicit version is using
exit $?which will return the last executed command status with
#!/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 /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
#!/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
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.
exit code is used to state command or script execution is completed succesfully.
1exit code is used to state that there are general errors like
divide by zeroetc.
2exit 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.
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