- How do I get the list of exit codes (and/or return codes) and meaning for a command/utility?
- 4 Answers 4
- Are there any standard exit status codes in Linux?
- Are there any standard exit status codes in Linux?
- Part 1: Advanced Bash Scripting Guide
- Part 2: sysexits.h
- Listing all system exit status codes with descriptions
- Proper System Exit codes [duplicate]
- All possible exit codes for cp
How do I get the list of exit codes (and/or return codes) and meaning for a command/utility?
Is there a way I can do what stated in the title from the terminal commands, or will I have to look into the codes?
4 Answers 4
Exit codes indicates a failure condition when ending a program and they fall between 0 and 255. The shell and its builtins may use especially the values above 125 to indicate specific failure modes, so list of codes can vary between shells and operating systems (e.g. Bash uses the value 128+N as the exit status). See: Bash — 3.7.5 Exit Status or man bash .
In general a zero exit status indicates that a command succeeded, a non-zero exit status indicates failure.
To check which error code is returned by the command, you can print $? for the last exit code or $ which gives a list of exit status values from pipeline (in Bash) after a shell script exits.
There is no full list of all exit codes which can be found, however there has been an attempt to systematize exit status numbers in kernel source, but this is main intended for C/C++ programmers and similar standard for scripting might be appropriate.
Some list of sysexits on both Linux and BSD/OS X with preferable exit codes for programs (64-78) can be found in /usr/include/sysexits.h (or: man sysexits on BSD):
0 /* successful termination */ 64 /* base value for error messages */ 64 /* command line usage error */ 65 /* data format error */ 66 /* cannot open input */ 67 /* addressee unknown */ 68 /* host name unknown */ 69 /* service unavailable */ 70 /* internal software error */ 71 /* system error (e.g., can't fork) */ 72 /* critical OS file missing */ 73 /* can't create (user) output file */ 74 /* input/output error */ 75 /* temp failure; user is invited to retry */ 76 /* remote error in protocol */ 77 /* permission denied */ 78 /* configuration error */ /* maximum listed value */
The above list allocates previously unused exit codes from 64-78. The range of unallotted exit codes will be further restricted in the future.
However above values are mainly used in sendmail and used by pretty much nobody else, so they aren’t anything remotely close to a standard (as pointed by @Gilles).
In shell the exit status are as follow (based on Bash):
- 1 — 125 — Command did not complete successfully. Check the command’s man page for the meaning of the status, few examples below:
- 1 — Catchall for general errors
$ let "var1 = 1/0"; echo $? -bash: let: var1 = 1/0: division by 0 (error token is "0") 1
Missing keyword or command, or permission problem (and diff return code on a failed binary file comparison).
$ curl foo; echo $? curl: (6) Could not resolve host: foo 6
$ /dev/null $ /etc/hosts; echo $? -bash: /etc/hosts: Permission denied 126
$ foo; echo $? -bash: foo: command not found 127
$ exit 3.14159 -bash: exit: 3.14159: numeric argument required
$ hexdump -n100000 /dev/urandom | tee &>/dev/null >(cat > file1.txt) >(cat > file2.txt) >(cat > file3.txt) >(cat > file4.txt) >(cat > file5.txt) $ find . -name '*.txt' -print0 | xargs -r0 cat | tee &>/dev/null >(head /dev/stdin > head.out) >(tail /dev/stdin > tail.out) xargs: cat: terminated by signal 13 $ echo $ 0 125 141
$ sleep 5 && killall sleep & [1] 19891 $ sleep 100; echo $? Terminated: 15 143
$ sh -c 'exit 3.14159'; echo $? sh: line 0: exit: 3.14159: numeric argument required 255
According to the above table, exit codes 1 — 2, 126 — 165, and 255 have special meanings, and should therefore be avoided for user-specified exit parameters.
Please note that out of range exit values can result in unexpected exit codes (e.g. exit 3809 gives an exit code of 225, 3809 % 256 = 225).
Are there any standard exit status codes in Linux?
System calls are when a program makes a request (in)to the kernel and are not the same as command invocation, thus these return codes are not (necessarily) the same as command exit codes. Some of these commands might be standardized or at least coordinated for several commands or a group of commands (e.g. » -compatible shells», I could imagine), but unless a command wants to conform to one of these conventions (and there are probably multiple conflicting conventions around), the command’s authors are completely free to decide what they want their exit status codes to mean.
Are there any standard exit status codes in Linux?
Part 1: Advanced Bash Scripting Guide
As always, the Advanced Bash Scripting Guide has great information: (This was linked in another answer, but to a non-canonical URL.)
1: Catchall for general errors
2: Misuse of shell builtins (according to Bash documentation)
126: Command invoked cannot execute
127: «command not found»
128: Invalid argument to exit
128+n: Fatal error signal «n»
255: Exit status out of range (exit takes only integer args in the range 0 — 255)
Part 2: sysexits.h
The ABSG references sysexits.h .
$ find /usr -name sysexits.h /usr/include/sysexits.h $ cat /usr/include/sysexits.h /* * Copyright (c) 1987, 1993 * The Regents of the University of California. All rights reserved. (A whole bunch of text left out.) #define EX_OK 0 /* successful termination */ #define EX__BASE 64 /* base value for error messages */ #define EX_USAGE 64 /* command line usage error */ #define EX_DATAERR 65 /* data format error */ #define EX_NOINPUT 66 /* cannot open input */ #define EX_NOUSER 67 /* addressee unknown */ #define EX_NOHOST 68 /* host name unknown */ #define EX_UNAVAILABLE 69 /* service unavailable */ #define EX_SOFTWARE 70 /* internal software error */ #define EX_OSERR 71 /* system error (e.g., can't fork) */ #define EX_OSFILE 72 /* critical OS file missing */ #define EX_CANTCREAT 73 /* can't create (user) output file */ #define EX_IOERR 74 /* input/output error */ #define EX_TEMPFAIL 75 /* temp failure; user is invited to retry */ #define EX_PROTOCOL 76 /* remote error in protocol */ #define EX_NOPERM 77 /* permission denied */ #define EX_CONFIG 78 /* configuration error */ #define EX__MAX 78 /* maximum listed value */
8 bits of the return code and 8 bits of the number of the killing signal are mixed into a single value on the return from wait(2) & co..
#include #include #include #include #include #include int main() < int status; pid_t child = fork(); if (child
How are you determining the exit status? Traditionally, the shell only stores an 8-bit return code, but sets the high bit if the process was abnormally terminated.
$ sh -c 'exit 42'; echo $? 42 $ sh -c 'kill -SEGV $$'; echo $? Segmentation fault 139 $ expr 139 - 128 11
If you're seeing anything other than this, then the program probably has a SIGSEGV signal handler which then calls exit normally, so it isn't actually getting killed by the signal. (Programs can chose to handle any signals aside from SIGKILL and SIGSTOP .)
'1' : Catch-all for general errors
'2' : Misuse of shell builtins (according to Bash documentation)
'126' : Command invoked cannot execute
'127' : "command not found"
'128' : Invalid argument to exit
'128+n' : Fatal error signal "n"
'130' : Script terminated by Ctrl + C
'255' : Exit status out of range
This is for Bash. However, for other applications, there are different exit codes.
Linux - Listing all system exit status codes with, The exit status is a numeric value that is returned by a program to the calling program or shell. In C programs, this is represented by the return value of the main () function or the value you give to exit (3). The only part of the number that matters are the least significant 8 bits, which means there are only values …
Listing all system exit status codes with descriptions
The exit status is a numeric value that is returned by a program to the calling program or shell. In C programs, this is represented by the return value of the main() function or the value you give to exit(3) . The only part of the number that matters are the least significant 8 bits, which means there are only values from 0 to 255 .
Code Description 0 success 1-255 failure (in general) 126 the requested command (file) can't be executed (but was found) 127 command (file) not found 128 according to ABS it's used to report an invalid argument to the exit builtin, but I wasn't able to verify that in the source code of Bash (see code 255) 128 + N the shell was terminated by the signal N (also used like this by various other programs) 255 wrong argument to the exit builtin (see code 128)
The lower codes 0 to 125 are not reserved and may be used for whatever the program likes to report. A value of 0 means successful termination, a value not 0 means unsuccessful termination. This behaviour (== 0, != 0) is also what Bash reacts on in some code flow control statements like if or while .
The above excerpt taken from Exit Status section from Bash Hackers Wiki.
There can be no comprehensive list, because the meaning of command exit statuses is inherently command-specific. For a given command, you can usually get information about this on the respective command's manual page and Info documents.
test$ hello -bash: hello: command not found test$ echo $? 127
the exit code 127 comes from bash , because the requested command itself couldn't be found.
test$ expr 1 / 0 expr: division by zero test$ echo $? 2
the exit code 2 comes from expr .
Some of these commands might be standardized or at least coordinated for several commands or a group of commands (e.g. " sh -compatible shells", I could imagine), but unless a command wants to conform to one of these conventions (and there are probably multiple conflicting conventions around), the command's authors are completely free to decide what they want their exit status codes to mean.
There's one important exception: All UNIX commands should adhere to this loose rule to be good citizens and provide meaningful composability (e.g. with pipes) on the command line:
- 0 means 'success' or "true"/"truthy"
- non- 0 means (in a very broad sense) 'failure' or 'non-success' or "false"/"falsy"
As you can see, this still leaves a lot of room for interpretation, which is perfectly intended, because these meanings must be specific to the context of the individual commands. (Consider e.g. the false command, that has the very purpose to "fail", thus always returns a non- 0 exit code.)
The list you found describes return codes for system calls. System calls are when a program makes a request (in)to the kernel and are not the same as command invocation, thus these return codes are not (necessarily) the same as command exit codes.
The list you showed is really the closest possible thing to a "standardization", but frankly it looks more legit than it actually is. As far as I am aware of, almost no one pays much attention to these guys, but instead everyone names their own exit statuses:
#!/bin/bash a=10 ; [ "$a" -eq 9 ] && echo "Cool!" || exit 200
Are there any standard exit status codes in Linux?, There are no standard exit codes, aside from 0 meaning success. Non-zero doesn't necessarily mean failure either. Header file stdlib.h does define EXIT_FAILURE as 1 and EXIT_SUCCESS as 0, but that's about it.. The 11 on segmentation fault is interesting, as 11 is the signal number that the kernel uses to kill the process in the event of a segmentation fault. Code samplewaitpid(child, &status, 0);if (WIFEXITED(status))printf("first child exited with %u\n", WEXITSTATUS(status));child = fork();if (child
Proper System Exit codes [duplicate]
EXIT_SUCCESS (i.e. 0) and EXIT_FAILURE (1) are standard, in ). Read also exit(3) and _exit(2) man pages. Notice that the argument of exit function is bit-or-ed with 0377 octal.
Some BSD systems have more conventions (perhaps in a header, see sysexits man page), but GNU/Linux does not follow them a lot.
I suggest sticking to EXIT_FAILURE -on failure- but giving, if possible or relevant, an error message (to stderr or thru syslog(3) . )
Some commands document their few exit codes.
Record exit codes of all shell commands as they are, Are there any standard exit status codes in Linux? 1153. How to echo shell commands as they are executed. 1125. Propagate all arguments in a bash shell script. 465. How to execute mongo commands through shell scripts? 480. Running multiple commands in one line in shell. 903.
All possible exit codes for cp
Here is how cp from coreutils-8.21 exits:
exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
There's nothing else than 0 or 1.
On Mac (10.12.5 (16F73)) there are additional codes. For instance:
Mac$ cp -W 1 2; echo $? cp: illegal option -- W usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file target_file cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file . target_directory 64
Linux - What are the exit codes in the RPM binary?, Each time rpm command returns different exit codes. For example- In case of failed dependency sometimes echo $? gives 1 and sometime 5. Are there any standard exit status codes in Linux? 1018. What is the meaning of "POSIX"? 183. Is there a tool or script that can very quickly find duplicates by …