Understanding Exit Codes
You’ve already seen some examples of using the exit
command, which can either terminate a script normally or cause it to terminate early upon an error condition. What I haven’t explained yet, is about exit codes. There are two general classes of exit codes, which are:
- Standard shell exit codes: Each shell has its own defined set of exit codes. (To keep things simple, I’ll just talk about
bash
exit codes in this chapter.) - User-defined exit codes: You can also define your own exit codes for different purposes.
Let’s talk about standard exit codes first.
Standard Shell Exit Codes
When a program or script runs successfully, it returns an exit code of 0
. Otherwise, the exit code will be a non-0
number from 1
to 255
. To demonstrate, use find
to search through the /etc/
directory for the passwd
file, like so:
[donnie@fedora ~]$ find /etc -name passwd
find: '/etc/audit': Permission denied...