Linux Bash exit and Exit Codes – POFTUT

Linux Bash exit and Exit Codes

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.

  • Or we can explicitly use the exit keyword in order to send the staus of the ls command like below.

  • Or the most explicit version is using exit $? which will return the last executed command status with $? by providing 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 $?

Print Last Command Exit Status

Print Last Command Exit Status

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 .

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.

echo '

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.
  • 1 exit code is used to state that there are general errors like divide 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.

LEARN MORE  How To Linux Bash For Loop In Files?

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.

Set Exit Status Into A Variable

Set Exit Status Into A Variable

As we can see the exit status is saved into variable named last_exit_status and get the exit value 1

1 Response

  1. 11/10/2019

    […] Bash is a utility to manage Linux and UNIX boxes and it is very popular because of its simple, well documented and unique usage. There are alternatives to bash like csh,sh,ksh but bash has very high usage rate against others. I have recently work on bash with its manual and wanted to write something that I have learned from them. By the way, bash is an acronym for “Bourne again shell”. […]

Leave a Reply

Your email address will not be published. Required fields are marked *