Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lookup the meaning of exit codes for Linux command line utilities?

Tags:

bash

I have my prompt (bash) configured to print out the exit code from the last command, if it wasn't successful (aka not zero). Therefore I'm seeing a lot of exit codes, even when a program seems to encounter no problems. Is there a way to lookup the meaning of these exit codes?

I always try the man pages, info pages or the "--help" option, but to no avail.

To clarify, I'm asking about the utilities that come with Linux, like cd, ls, du, ...

like image 423
chrm Avatar asked Sep 03 '11 15:09

chrm


People also ask

How do I find exit code in Linux?

Checking Bash Exit Code Launch a terminal, and run any command. Check the value of the shell variable “$?” for the exit code. $ echo $? As the “date” command ran successfully, the exit code is 0.

How do I find the exit code for a script?

To check the exit code we can simply print the $? special variable in bash. This variable will print the exit code of the last run command. $ echo $?

How do I find exit code from previous command?

Extracting the elusive exit code To display the exit code for the last command you ran on the command line, use the following command: $ echo $? The displayed response contains no pomp or circumstance. It's simply a number.

What are exit codes in Linux?

In Linux, an exit code indicates the response from the command or a script after execution. It ranges from 0 to 255. The exit codes help us determine whether a process ran: Successfully.


2 Answers

There isn't a standardized meaning for the exit codes of programs beyond '0 is OK; anything else means something went wrong'. And strictly, that applies to C and C++ only - there, exit(0); or exit(EXIT_SUCCESS); both exit with success, but the value returned to the O/S might be different.

There are exceptions to even the zero-on-success rule. Obviously, there are careless programs that don't return a defined exit status; such programs are best avoided. There are other not quite so careless programs that always return 0, even when something went wrong; they too are often best avoided.

However, there are also programs that carefully encode quite a lot of information into the exit status, and simply getting a non-zero exit status does not mean such programs failed. Of course, the programs document the meanings of the exit statuses.

POSIX is careful to document exit statuses for programs.

The manual pages for a program should document the exit statuses. On Unix, if there is no such documentation, then assume zero-on-success and anything else is a failure.

Note that if bash fails to execute a command, it will return stylized statuses:

  • 127 if the file does not exist or cannot be found
  • 126 if you do not have execute permission on the file

Also, if the program dies because of a signal, bash lets you know by encoding the exit status as:

  • 128 + signal-number

Hence SIGHUP yields 129, SIGILL yields 132, SIGTERM yields 143, etc. However, it is possible for a program to exit with any of those statuses and mean something different from what bash means. That said, it is a relatively unusual program that exits with any of those exit statuses, so you're usually safe.

Note that different operating systems have different conventions: Unix provides an 8-bit status with zero for success; Windows provides a much larger range for the exit status values (16-bits or 32-bits); VMS used zero to mean failure, I believe.

like image 66
Jonathan Leffler Avatar answered Sep 23 '22 07:09

Jonathan Leffler


The man pages are the conventional place for this documentation. If you are on e.g. Debian, you would even be expected to file a bug report against utilities which have undocumented exit codes.

like image 33
tripleee Avatar answered Sep 20 '22 07:09

tripleee