Bash Shell Exit Status Tutorial with Practical Examples

by Sasikala on March 26, 2010

In our bash introduction article, we learned that a shell-script file contains list of commands to be executed by the shell interpreter. In this article let us review about shell commands and its internals.

A command is a sequence of words. The first word indicates the command to be executed and remaining words are passed as an arguments, where arguments could be the options or parameters to the command.

Some of the common Unix commands you execute at the command line are shell commands. For example, ls, lpr and grep command.

$ ls -alF

$ lpr filename

$ grep "string" filename

Shell Command Exit Status

The return value of a command is its exit status, or 128 + N if the command is terminated by signal N. Exit status is used to check the result (success/failure) of the execution of the command. If the exit status is zero, then the command is success. If the command is failed the exit status will be non-zero.

Exit Value Exit Status
0 (Zero) Success
Non-zero Failure
2 Incorrect usage
127 Command Not found
126 Not an executable

$? Shell Variable

The shell variable name $? is a special built-in variable which has the exit status of the last command executed.

  • After the shell function execution, $? returns the exit status of the last command executed in a function.
  • After the shell script execution, $? returns the exit status of the last command executed in the script.

Sample Shell Script that Explains Shell-Command Exit Status

The following exitstatus.sh shell-script shows examples of various shell-command exit status.

$ cat exitstatus.sh
#! /bin/bash

echo -e "Successful execution"
echo -e "====================="
echo "hello world"
# Exit status returns 0, because the above command is a success.
echo "Exit status" $? 

echo -e "Incorrect usage"
echo -e "====================="
ls --option
# Incorrect usage, so exit status will be 2.
echo "Exit status" $? 

echo -e "Command Not found"
echo -e "====================="
bashscript
# Exit status returns 127, because bashscript command not found
echo "Exit status" $? 

echo -e "Command is not an executable"
echo -e "============================="
ls -l execution.sh
./execution.sh
# Exit status returns 126, because its not an executable.
echo "Exit status" $?

Now, execute the above exitstatus.sh to see the various exit statues given by the sample shell script.

$ bash exitstatus.sh
Successful execution
=====================
hello world
Exit status 0
Incorrect usage
=====================
ls: unrecognized option `--option'
Try `ls --help' for more information.
Exit status 2
Command Not found
=====================
exitstaus.sh: line 15: bashscript: command not found
Exit status 127
Command is not an executable
=============================
-rw-r--r-- 1 root root 659 Mar  9 13:36 execution.sh
exitstatus.sh: line 21: ./execution.sh: Permission denied
Exit status 126

Note: Checking the return value of a function or a command is one of the main responsibility of a programmer. This should become your second nature while writing any code.


Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 4 comments… read them below or add one }

1 Shantanu Oak March 26, 2010 at 8:44 am

I will like to add 3 points.
1) You need to type $? immediately after running the command.
2) Do not forget that you can save the exit code to a variable like myvar=$? and
3) It shows the exit code of the last command in case of broken pipes. For e.g.
some_command | ls
The exit code of the above command is 0 and not 127 as in case of some_command && ls

2 Catalin March 28, 2010 at 7:14 am

Try this :
$aaa
$echo “Exit status” $?
Exit status 130
;)

3 carlos April 29, 2010 at 9:24 am

Hi Catalin.

Your comment including exit status sounds like a bug in shell (they exist, sure!!).

Maybe when you use $aaa like a command (I presume $aaa is null), the exit status was 2. And when you print $0, the mere act of doing so makes $0=$0 + 128 …. I guess??

Even when I did not documented, I have found some curiosities like this one. With a little of effort you can get results, maybe doing the same task with other instructions, maybe taking the error and making a variable take the value of the error, …

Any ideas?

carlos

4 Catalin April 30, 2010 at 10:27 am

Yes . is null , because is not declareted .
The command shell is bash Fedora 13.

Leave a Comment

Previous post:

Next post: