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.

# 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.

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?


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

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

  • That is simply a convention made-up by whoever wrote that tutorial, and you shouldn't think too much about it. On Unix, the exit status is either success (0) or failure (anything but 0). There were efforts to standardize a set of meaningful error exit statuses (look at /usr/include/sysexits.h), but they didn't really catch on. – mosvy Mar 6 '19 at 10:31
  • warning: the script's variables should be in lowercase... otherwise you risk "collision" with internal variables of your shell (ex here: LINES, which in many shells is (re-)set to the number of lines of the terminal emulator, and therefore could possibly change if the window is resized during the script's execution?) – Olivier Dulac Jul 8 '19 at 14:09

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

| improve this answer | |
  • 1
    Thank you for your feedback, Hello @Juan I know the exit status 0 and 1, what about the variable exit status 86, 87 actually this is reserved number or it's random. – Ajil Raju Mar 6 '19 at 18:13
  • @AjilRaju regarding to exit status, nothing is reserved. As every answer here says, the Exit status are "conventions". Every developer set a "meaning" for every exit status in their own software. Usually the developers document that meaning in the manual pages or in a text file distributed with the software. So practically speaking, we can say that they are random. – Juan Mar 7 '19 at 15:41

In general, the exit status of any process is defined by POSIX to be an 8-bit (unsigned) integer value, so the possible values are between 0 and 255 inclusive.

WEXITSTATUS(stat_val) [This] macro evaluates to the low-order 8 bits of the status argument that the child process passed to _exit() or exit(), or the value the child process returned from main(). wait(3p)

The C standard specifies two constants, EXIT_SUCCESS and EXIT_FAILURE, that may be passed to exit() to indicate successful or unsuccessful termination, respectively. exit(3)

Conventionally, EXIT_SUCCESS is equal to zero and all non-zero values are treated as a sign of an error.

However, POSIX-like shells including Bash reserve a range of high exit statuses for internal use, to signal that something went wrong in the called command without that command having a chance to return an explicit exit status (Bash manual):

For the shell's purposes, a command which exits with a zero exit status has succeeded. An exit status of zero indicates success. A non-zero exit status indicates failure. When a command terminates on a fatal signal N, bash uses the value of 128+N as the exit status. If a command is not found, the child process created to execute it returns a status of 127. If a command is found but is not executable, the return status is 126.

In practice, any value between 1 and 125 can thus be used to indicate an error in a program-specific manner. As @mosvy hinted in a comment, there was an effort by BSD to standardize some meaningful exit codes, but it never became universal. These unified exit codes started from 64, leaving anything below that value for program-specific use. The highest "standardized" value was 78, so it is clear that the values mentioned in your example are purely arbitrary program-specific codes.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.