Understand Exit Statuses in Bash

Cameron Nokes
InstructorCameron Nokes

Share this video with your friends

Send Tweet
Published 3 years ago
Updated 3 years ago

Every command in bash returns an exit status, which is an integer between 0 and 255 that represents whether the command succeeded or failed, and if it failed, what kind of error occurred. Functions also can return an exit status via the return keyword. The exit status can be checked using bash's special $? variable, which always stores the exit status of the last command run.

Instructor: [00:00] In bash, every script, command, or function that runs will return an exit status. See what that means. If we run ls, you can see I am in a directory here, which is just one file. To check the exit status of a command, we use the bash shell question mark variable that bash does for us.

[00:21] Our ls ran successfully, so returned a zero. An exit status is an integer between 0and 255. It tells that the program ran successfully, and if not, what sort of error was encountered.

[00:33] See what it looks like if we do something that intentionally won't work. If you ls on a photo that doesn't exist, it will return an error. Let's see what the exit status was.

[00:46] We got one, which is what we expect. One just means a general error. The most common exit statuses return are zero and one. It's up to your command to define what is an error or not.

[00:59] See another example. Let's run sleep for 10 seconds. Sleep will just start an idle process that just hangs for this number of seconds. While this is running, if I do control c, it will terminate that process.

[01:14] It's sense an interrupt signal to the process, and different signals that you can send to the process. Here's the result and different exit statuses.

[01:22] If I check the exit status for that, you can see it's 130. See how exit statuses work in scripts. I've already created a script called script.sh and it has execute permissions on it. In here, if I just call exit and pass one, this will set the exit status for the script.

[01:44] Let's write that out. Let's revoke it. We'll check the exit status, and it's one like we expect. If we don't set an exit status explicitly, it's assumed to be zero or it's the results of the last command that was run.

[01:58] Let's go in here. Let's see how exit statuses work with functions. I am going to create an OK function. Instead of using exit here, because exit will cause entire script to stop and exit out, use the return keyword. For OK, I am going to return zero, and then for my fail function here, I am going to return one.

[02:21] I am going to call fail first, and OK second. Let's run this. I run it. Let's check the exit status on it. We can see it's a zero even though we have that failing function in there. It's because the last run command gets set as the exit status for the script.

[02:38] If we come in here and if we echo like this, we'll see the exit status of each of these functions. This question mark variable is set for every command or function that's run. Run script again. We can see that it's a fail function, the OK function, and then the entire script of course will have a zero exit status.