120

I'm doing some practice exercises.

Write a script that will be given a month number as the argument and will translate this number into a month name. The result will be printed to stdout.

I made a solution:

# Test for number of argument

if [ "$#" -eq 0 ] ; 
then
  echo -e "No argument."
  echo -e "Write a number between 1 and 12."
  exit 1
elif [ "$#" -gt 1 ] ;
then
  echo -e "More than 1 argument."
  echo -e "Write a number between 1 and 12."
  exit 1
else
  numb=$1
  case "$numb" in
    1) echo "Month: January";;
    2) echo "Month: February";;
    3) echo "Month: March";;
    4) echo "Month: April";;
    5) echo "Month: May";;
    6) echo "Month: June";;
    7) echo "Month: July";;
    8) echo "Month: August";;
    9) echo "Month: September";;
   10) echo "Month: October";;
   11) echo "Month: November";;
   12) echo "Month: December";;
    *) echo -e "You wrote a wrong number. Try again with writing number between 1 and 12.";;
  esac
fi
exit 2
exit 0

What do exit 1, exit 0 and exit 2 mean, and why do we use them?

4
  • 5
    A successfully executed code should exit with code 0. Other values indicate an error. See for instance Exit Codes With Special Meanings
    – V. Olsen
    Mar 13, 2017 at 15:21
  • 2
    A bash script is like a movie theater, there are different exists. The exit statements mark the location of the exits for the bash interpreter. When the script is fired, the interpreter is required to run until the nearest exit. Mar 13, 2017 at 23:51
  • 5
    If this is the code you have written, how come you have used statements you don't even understand? Mar 14, 2017 at 11:09
  • 6
    1) You accepted an answer right after posting the question. Give it 24 hours to wait for even better answers. 2) The accepted answer is wrong and misleading. The correct answer is "there is no default meaning for the exit codes (beyond 0 = success); you as script developer define the semantics". 3) If you made that script, why did you add the exit commands (all of which are logically superfluous or could collapse to "exit 1" since you use if-else anyway).
    – AnoE
    Mar 14, 2017 at 13:30

4 Answers 4

140

Here's one good reference for shell exit codes:

Exit code 0        Success
Exit code 1        General errors, Miscellaneous errors, such as "divide by zero" and other impermissible operations
Exit code 2        Misuse of shell builtins (according to Bash documentation)        Example: empty_function() {}

Caveat: Using the proper exit code is not a requirement and is not enforced by the shell. Developers can ignore the guidance if they think it wise.

2
  • 19
    Be careful, though. What an exit code means is entirely up to the developers of the program.
    – muru
    Mar 14, 2017 at 4:28
  • Also, while the linked source, the Advanced Bash Scripting Guide, claims to show "Reserved Exit Codes" (emphasis in original), that claim is both uncited and entirely false. Aug 4, 2019 at 14:49
26

Using exit and a number is a handy way of signalling the outcome of your script. It mimics the way that bash commands output a return code. With bash commands the return code 0 usually means that everything executed successfully without errors. exit also makes your script stop execution at that point and return to the command line.

Any return code greater than 0 indicates an error of some sort, though sometimes the error isn't critical, for each command it should be possible to find some documentation that tells you what each return code means.

You can get the return code of the last bash command by using the shell variable $? like so:

$ echo "something"
something
$ echo $?
0
$ cp
cp: missing file operand
Try 'cp --help' for more information.
$ echo $?
1

When you use this in a script, you can query the return code in the same way when it's done executing. So you'll see that having:

exit 2
exit 0

Is sort of meaningless, as you can never reach the exit 0 part.

6

All by itself, exit implies an exit value of zero, or successful completion of your script. You do not have to add the zero argument to the exit command to indicate successful completion. Your script may (or probably will) exit successfully although it tests for an erroneous condition. In this case you specifically want it to exit with the error (or 1) condition.

echo -e "Enter numbers 1-4" \c"
read NUM
case $NUM in 
    1) echo "one";;
    2) echo "two";;
    3) echo "three";;
    4) echo "four";;
    *) echo "invalid answer"
       exit 1;;
esac

The exit command in the last line does not have to be there at all. It could be called with the zero or not called at all. Without specifying the 1 argument to the exit command the answer in all these cases to the echo $? would be zero.

However by specifying the 1 argument to the exit command the response to the echo $? would be 1. So use the 1 argument to the exit command when you want to specify that the script has exited with an error condition.

0

This is (still) the case statement exercise in LinuxFoundation's LFS101x, Introduction to Linux (free course). The code above has exit 2 in the wrong place, making the exit statements meaningless (as pointed out in original responses). In the provided solution to the exercise, exit 2 is included in the case statement's last case, "*", so the case statement exits with an error if given invalid input. Then, after the esac, is exit 0, indicating successful completion of the code if the invalid case (and exit 2) are bypassed.

screenshot of relevant lines of code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .