Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am confused about the meaning of the exit code in the end of a bash script: I know that exit code 0 means that it finished successfully, and that there are many more exit codes numbers (127 if I'm not mistaken?)

My question is about when seeing exit code 0 at the end of a script, does it force the exit code as 0 even if the script failed or does it have another meaning?

share|improve this question
2  
If a script ends with exit 0, it will exit with the exit code of 0 regardless of what happens within the script. – Hatclock Sep 6 '16 at 14:26
    
@Hatclock why didn't you post your answer a full pledged answer ? as a comment I cant mark it, and I don't think you get fake internet points for answering (right?) Just a thought... (kudos for the fast reply) – Busted Sep 6 '16 at 14:46
1  
I'm at a train station on my phone, I don't really like typing too much on my phone. ilkkachu seems to have a good answer though! – Hatclock Sep 6 '16 at 14:55
    
1  
@Hatclock No, not at all. If a script ends with exit 0, it will exit with the code 0 only if that last instruction was executed. The only impact of exit 0 at the end of the script is to return 0 instead of the status from the previous instruction. – Gilles Sep 6 '16 at 22:08
up vote 3 down vote accepted

The builtin command exit exits the shell:

exit [n]
Exit the shell, returning a status of n to the shell’s parent. If n is omitted, the exit status is that of the last command executed. Any trap on EXIT is executed before the shell terminates.

Running to the end of file also exits, returning the return code of the last command, so yes, a final exit 0 will force the script to exit with successful status. The exit codes of any earlier commands don't matter. (At the end of a script you could also use true or : to get an exit code of zero.) Of course more often you'd use exit from inside an if to end the script in the middle.

These should print a 1 ($? contains the exit code returned by the previous command):

sh -c "false" ; echo $?
sh -c "false; exit" ; echo $?

While this should print a 0:

sh -c "false; exit 0" ; echo $?

I'm not sure if the concept of the script "failing" when executing an exit makes sense, as it's quite possible to some commands ran by the script to fail, but the script itself to succeed. It's up to the author of the script to decide what is a success and what isn't.

Oh, and, exit codes are 8 bits wide, so the range is 0..255. Codes above 127 are used when a process is terminated by a signal, but they can be returned in the usual way. The wait system call actually a value with a larger range, the rest contains basically status bits set by the kernel.

share|improve this answer
    
Cool, I didn't know about the 8 bits exit codes numbering. Thanks! – Busted Sep 6 '16 at 15:30
    
Note that when a process is killed, it does not exit with an exit code > 127. It's just that some shells set $? to 128+signum in that case. See Default exit code when process is terminated? for details. – Stéphane Chazelas Sep 6 '16 at 16:13

0 means success, positive integers mean failure. There are 255 different error codes, but values 126 and above are reserved to indicate that a program couldn't start (126 or 127) or was killed by a signal (129 and above). See Default exit code when process is terminated? and What return/exit values can I use in bash functions/scripts? for more information.

The exit status of a shell script is the exit status of the last command that the script executed. So for example

#!/bin/sh
somecommand

returns the exit status of somecommand, whereas

#!/bin/sh
somecommand
exit 0

returns 0 regardless of what somecommand returned. This second script could also be written

#!/bin/sh
somecommand
true

Putting exit 0 at the end of a script doesn't necessarily cause it to return 0. This only makes it return 0 when the end of the script is reached. For example, the following script always returns 3:

#!/bin/sh
exit 3
exit 0

The following script also always returns an error code, in addition to displaying a message about a syntax error:

#!/bin/sh
}
exit 0

The following script returns either 1 or 0 depending on its first argument:

#!/bin/sh
if [ "$1" = "foo" ]; then
  exit 1
fi
exit 0

The following script returns the status of somecommand, since set -e causes the script to exit if somecommand fails:

#!/bin/sh
set -e
somecommand
exit 0
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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