01. Exit Statuses

When a program finishes, it returns a integer value. This code is known as the exit status or exit code. Depending on its value, we can see if the program terminated successfully or not.

If the value is 0, that means the program ran without a problem (this is why you return 0 in C programs). However, any value of 1 or greater means that it terminated unsuccessfully.

So...what do exit statuses have to do with control flow? Great question! We'll how all this ties in together real soon when we learn if-else statements.

Last exit status $?

To find the last exit status, simply use the $? variable.

Be cautious in that if you run echo $? twice in a row, the second command will always return a 0. Can you guess why?

Let's try some examples.

$ rm /; echo $?
rm: /: is a directory
1
$ ls -l; echo $?
# Lists folder contents
0
$ echo $?
# Running 'echo $?' twice always produces a success
0

POSIX exit statueses

Here is a list of POSIX exit statuses.

0
Success
> 0
Failure due to redirection or word expansion.
1-125
Unsuccessful (for the most part, but depends on command).
126
File not executable.
127
Command not found.
128
Unspecified, but failure.
>128
Command failed due to receiving a signal.
$ ls -l hello_world
# Not executable
-rw-r--r--  1 johnPC  staff  0 Jul  3 09:41 hello_world
$ ./hello_world
-bash: ./hello_world: Permission denied
$ echo $?
126
$ asdf
$ echo $?
127

Passing an exit value per script

To terminate your script with a specific exit code, use the exit code, followed by an integer.

if [ ! -e $SAMP_FILE ]; then
    exit 32
fi

We'll learn if statements real soon, but this snippet simply checks if the file does not exist.

Exceptions to the 0 successful rule

In some cases, a program can exit with a value other than 0, and still be considered a successful run.

For example, the grep command returns a 0 if a matching pattern is found, and a 1 if no matching pattern is found. A value of 2 or greater is reported for real errors.

To ensure of no bugs in your code, make sure to check out a command's EXIT VALUE or DIAGNOSTICS section of its man page!

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

How Linux Works

Take your Linux skills to the next level! Try Linux & UNIX

In this completely revised second edition of the perennial best seller How Linux Works, author Brian Ward makes the concepts behind Linux internals accessible to anyone curious about the inner workings of the operating system. Inside, you'll find the kind of knowledge that normally comes from years of experience doing things the hard way.

$ Check price
39.9539.95Amazon 5 logo(114+ reviews)

More Linux & UNIX resources

Ad