9

I'm having difficulty locating a comprehensive up-to-date list of error codes from Bash. e.g.:

$ udevadm info /dev/sdx; echo Exit code $?
Unknown device, --name=, --path=, or absolute path in /dev/ or /sys expected.
Exit code 4

How is one supposed to look up such exit codes?

Dave Rove
  • 1,315

2 Answers2

13

There are a handful of exit codes with reserved special meanings:

Exit Code Number   Meaning
1      Catchall for general errors
2      Misuse of shell builtins (according to Bash documentation)
126    Command invoked cannot execute
127    "command not found"
128+n  Fatal error signal "n"
130    Script terminated by Ctrl-C
255*   Exit status out of range

Everything below 125 is fair game for developers, and can really only be divined, as l0b0 notes in his answer, by reading the man page for the application, or the source code, to determine what the code signifies (if, indeed, it is documented at all).

jasonwryan
  • 73,126
  • 2
    It should also be noted that these are "reserved" only by convention - there is no enforcement (other than good sense) actually preventing, for example, an application using 1 or 2 to mean success. Everything is actually fair game for developers. – nobody Oct 17 '14 at 16:12
  • To expand, they "should [...] be avoided for user-specified exit parameters." – l0b0 Oct 17 '14 at 17:48
  • @AndrewMedico This isn't quite true: 0 for success isn't just a matter of convention, it's built into a lot of tools including shells (&&, ||, !, set -e, …). – Gilles 'SO- stop being evil' Oct 17 '14 at 23:03
  • The entries for 1 and 2 don't make much sense. 1 and 2 are not reserved. Bash builtins don't even always use 2 — in fact they seem to use 1 for errors; other shells use different error codes (usually 1 or 2). “Missing keyword” and diff aren't about shell builtins. 1 and 2 are both common catchalls. – Gilles 'SO- stop being evil' Nov 16 '14 at 08:35
12

tl;dr Exit codes are application specific.

There are some loose conventions. false and anything successful prefixed with ! (like ! true) in POSIX shells return exit code 1, but a developer can use any exit code between 0 and 255 for whatever they want. Ultimately you have to look at its documentation (in the best case) or the code (in the worst case) to know what it means. For programs with man pages exit codes will often be listed in a section named EXIT STATUS (GNU tools like find).

Some popular meanings are listed in /usr/include/sysexits.h - I try to use them whenever possible. As @AnsgarEsztermann points out, these are not a Bash reference, or even an application reference except for those who choose to use it (C/C++ developers primarily according to the ABS).

l0b0
  • 51,350
  • I see. Thanks l0b0 and jasonwryan. So I conclude that particular command is improperly documented. – Dave Rove Oct 17 '14 at 08:53
  • 2
    In case this has not become clear from l0b0's answer: these exit codes are not "bash exit codes". They belong entirely to the exiting commands, and they will not change if these commands are called in a different way that does not involve bash. – Ansgar Esztermann Oct 17 '14 at 09:10
  • The values in sysexits.h are pretty much unused except by procmail and company. Only sendmail (and some other MTAs) care about them. – Gilles 'SO- stop being evil' Nov 16 '14 at 08:36