What is the meaning of exit 0, exit 1, and exit 2 in a bash script?
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?
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.
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.
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.