Linux – What Is Exit Status and List of All Status Codes

bashexit-statuslinuxscriptingshell-script

I'm trying to learn Linux bash scripting and I'm read through the articles in the http://tldp.org sites I saw some kind log clearing scripting I notice that something is used for as an exit status.
I have given below a few script snippet from the article.

#!/bin/bash
# Cleanup, version 3

#  Warning:
#  -------
#  This script uses quite a number of features that will be explained
#+ later on.
#  By the time you've finished the first half of the book,
#+ there should be nothing mysterious about it.

LOG_DIR=/var/log
ROOT_UID=0     # Only users with $UID 0 have root privileges.
LINES=50       # Default number of lines saved.
E_XCD=86       # Can't change directory?
E_NOTROOT=87   # Non-root exit error.

What do the E_NOTROOT(86) and E_XCD(87), if both variables use the reserved
exit status code for the program or not?

OR

If both variables just use the random number for this purpose.

Reference: http://tldp.org/LDP/abs/html/abs-guide.html

Best Answer

Every execution has an Exit status. In general, zero means OK and non-zero is an error. That value is not shown naturally in the standard output. You can see that value typing echo $? after every executed command.

For example if you type:

mkdir test;echo $? If you have the right perms, you will create the directory and then you will see a zero.

but if you write mkdir testing/test;echo $? having the right perms but without having the "testing" subdir, you will see the error message and then a "1".

This is a very important tool in GNU/Linux because the commands can be interconnected. And (just as example) if you use double-ampersand for connecting commands, the second command ONLY is executed if the first command has a zero value on the exit. There are many ways to connect commands. To learn more just type man bash

Related Question