Concept

Exit Codes

Definition

An exit code (also called an exit status or return code) is a small integer that a process hands back to its parent when it terminates. The convention is rigid and universal across Unix-derived systems: zero means success, any non-zero value means failure. The specific non-zero value typically encodes which kind of failure occurred, allowing calling scripts to react differently to a missing file, a permission denial, or a malformed argument.

The exit code is the only structured information a parent process is guaranteed to receive from a child after the child has exited. Standard output and standard error carry human-readable details; the exit code carries the machine-readable verdict.

Why it matters

How it works

When a process terminates, the kernel records its exit status in a per-process structure and notifies the parent via the SIGCHLD signal. The parent collects the status by calling wait or waitpid, which returns a packed integer encoding both the exit value and whether the child died from a signal. In the shell, the most recent exit code lives in the special parameter dollar-question-mark, which scripts test directly or feed into conditional operators.

The conventions are layered on top of this simple integer return. Beyond the success-zero rule, BSD and Linux distributions standardised a set of meaningful codes — 64 for usage error, 65 for data format error, 66 for missing input — defined in sysexits.h. Most user-written tools ignore these and just use 1 for everything that went wrong, but well-behaved utilities like grep distinguish "no match found" (exit 1) from "actual error" (exit 2), and that distinction lets calling scripts respond appropriately.

Where it goes next

Continue exploring

Tags