Exit Codes and Conventions for Unix Tools
What exit status 0, 1, 2, 126, 127 and 128 plus a signal number mean on Linux, how a shell reads them, and which codes your own command-line tool should return.

Every program on a Unix system ends by handing a small number back to whoever started it. A shell uses that number to decide whether to run the next command, a build system uses it to decide whether to stop, and a monitoring job uses it to decide whether to page someone. Get it wrong and a script keeps going after a failure, or aborts on a harmless warning. This post explains what the number is, which values have a fixed meaning on Linux with a POSIX shell, and which ones your own tool should return, building on the structure in the guide to command-line tools in C.
What an exit status is
When a process ends, the kernel keeps a record of how it ended until the parent collects it with wait or waitpid. The part for a normal exit is the low eight bits of the value passed to exit, so the range is 0 to 255. A program that calls exit(256) reports 0, and exit(-1) reports 255. Shells expose the value of the last command as $?.
true
echo $?
false
echo $?0
1The commands true and false exist for exactly this purpose: they do nothing and return 0 and 1. A shell treats 0 as success and any other value as failure in an if, a while, && and ||. That inversion from the usual idea of zero as false is a regular surprise for C programmers, and it is the single most important convention here.
The fixed meanings
POSIX defines few exit codes, and the shell adds a few more.
| Code | Defined by | Meaning |
|---|---|---|
| 0 | POSIX, ISO C | Success |
| 1 to 125 | Free for the program | Failure of some kind, defined by the tool |
| 126 | POSIX shell | The command was found but could not be executed |
| 127 | POSIX shell | The command was not found |
| 128 + n | POSIX shell | The command was terminated by signal n |
| 255 | Convention | Exit status out of range, or fatal error in some tools |
The shell codes come from the section on the shell command language in POSIX.1-2024. If the shell cannot find foo, it returns 127 without running anything, and if it finds a file that is not executable, it returns 126. When a process dies from a signal, there is no exit code to report, so the shell reports 128 plus the signal number. A command killed by SIGINT (signal 2) from Ctrl-C shows 130, one killed by SIGTERM (15) shows 143, and one killed by SIGSEGV (11) shows 139.
sleep 100 &
kill -TERM $!
wait $!
echo $?143Your own tool must never return 126, 127 or a value above 128 to mean something else, because a script reading $? cannot tell your 130 from a real Ctrl-C.
Choosing codes for your own tool
Between 1 and 125 you are free, and that freedom tempts people to invent a table. The conventions that have settled are small.
1 means a general failure, and it is what most tools return for any error. It is the safe default.
2 means misuse of the command line: an unknown option, a missing operand, a bad value. The reasoning is that a script wants to distinguish "I called this wrong" from "the work failed". GNU
grepreturns 2 for errors and 1 for no match, anddiffreturns 2 for trouble and 1 for differences.Search tools often use 1 for "ran correctly, found nothing". If you build a tool that answers a yes-or-no question,
grepis the model: 0 for yes, 1 for no, 2 for error. Document it.
Beyond those, <sysexits.h>, a BSD header also found on Linux, defines named codes from 64 up: EX_USAGE (64), EX_DATAERR (65), EX_NOINPUT (66), EX_UNAVAILABLE (69), EX_SOFTWARE (70), EX_OSERR (71), EX_IOERR (74), EX_TEMPFAIL (75) and EX_CONFIG (78). Mail transfer agents and a few tools use them. They give more precise codes, but few scripts check them, and the header is not part of POSIX. For a general tool, 0, 1 and 2 are enough, and the extra detail goes in the message on standard error.
Returning codes from C
In C, either return from main or exit ends the process with a status. The portable constants live in <stdlib.h>: EXIT_SUCCESS is 0 and EXIT_FAILURE is 1 on every system you will meet. Use them for those two cases, and plain integers or your own named constants for others.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { EXIT_USAGE = 2 };
int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "usage: %s word\n", argv[0]);
return EXIT_USAGE;
}
if (strcmp(argv[1], "ok") == 0)
return EXIT_SUCCESS;
fprintf(stderr, "%s: not ok: %s\n", argv[0], argv[1]);
return EXIT_FAILURE;
}Run in a shell:
./status ok; echo $?
./status nope; echo $?
./status; echo $?0
./status: not ok: nope
1
usage: ./status word
2Returning from main and calling exit are the same thing, since main's return value is passed to exit, which flushes stdio and runs atexit handlers. A child process after fork that must not flush its parent's buffers calls _exit instead, so that buffered output is not written twice.
One more rule from the C standard: if main falls off its closing brace without a return, the result is 0 in C99 and later for main specifically. Relying on that is legal but invites confusion, and an explicit return is clearer.
Reading the status of a child
A parent that starts another program reads the status through waitpid. The raw value is a packed integer, and the macros from <sys/wait.h> pick it apart.
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void)
{
pid_t pid = fork();
if (pid == 0) {
execlp("false", "false", (char *)NULL);
_exit(127);
}
int st;
waitpid(pid, &st, 0);
if (WIFEXITED(st))
printf("exited with %d\n", WEXITSTATUS(st));
else if (WIFSIGNALED(st))
printf("killed by signal %d\n", WTERMSIG(st));
return 0;
}The program prints:
exited with 1WIFEXITED is true for a normal exit and WEXITSTATUS then gives the code. WIFSIGNALED is true when a signal ended the process and WTERMSIG names it. The two cases are exclusive, so a library that wraps waitpid should expose both. The _exit(127) after execlp follows the shell's rule that a failed exec is reported as "command not found", and it prevents the child from running the parent's code if exec returns.
Pipelines and set -e
A pipeline's status is, by default, the status of its last command. In a | b | c, a failure in a does not show in $? unless it also affects c. Bash offers set -o pipefail, which makes the pipeline fail if any stage failed, and the array PIPESTATUS records every stage's code. POSIX.1-2024 added pipefail to the standard shell, so scripts that use it are no longer bash-only.
set -e stops a script on the first failing command, with exceptions that have caused real bugs: commands in a condition (if cmd, cmd || other, cmd && other) do not trigger it. A tool should therefore return a clear nonzero status on any failure it detects, because it cannot know how it will be called.
Partial success
A tool that processes many inputs has to say what its status means when some work succeeded and some failed. The common rule is that any failure makes the exit status nonzero, even if every other file was processed. cp a b dir/ returns 1 if either copy fails. That keeps && chains honest, at the cost of making the status coarse. If the caller needs to know which items failed, the tool prints each failure on standard error, and a script that needs more detail parses that output or processes the files one at a time.
Some tools go further and give a separate status for warnings, as diff does for differences. That works when the extra meaning is documented and stable, and it fails when the next release adds one more value.
Mistakes that lose failures
Four habits make failures vanish.
Exiting 0 after printing an error. A tool that prints
error: cannot openand then continues toreturn 0makes the failure invisible to every script.Returning the wrong thing from a wrapper. A shell script that ends with
cleanupreturns the status ofcleanup, not of the work before it. Save the status withrc=$?andexit $rc.Returning a value over 255.
exit(1000)reports 232, the value modulo 256. Never returnerrno, which can exceed 255 on some systems, and do not pack a count of failures into the status.Ignoring write errors. A tool that cannot flush its output because the disk is full and still exits 0 has lost data silently. Check
fflushandferrorbefore the final return.


