linuxspeaks

HOW LINUX REPORTS WHAT HAPPENED

Zero means
it worked.

Every command you run in a Linux shell answers back with a number.

A laptop on a desk at night showing a dark terminal window with blurred lines of output, lit by a warm desk lamp
AI-generated image, not a photograph of a real person or place.

$? the last answer

0–255 Possible exit statuses

It is called the exit status. Zero means success; anything else means some kind of failure. Alongside it, programs print error messages built from a short list of standard error names, sent down their own separate stream.

THE SHORT ANSWER

A number,
then a message.

The exit status says whether it worked.
The error message says why it did not.

EXIT STATUS

0 to 255

The GNU Bash manual says exit statuses fall between 0 and 255. Zero means success, so there is one way to succeed and many ways to fail.

Decode a status

ERROR NAMES

ENOENT and friends

System calls set errno to a positive number with a standard symbolic name, such as ENOENT for No such file or directory.

Read an error

TWO STREAMS

Output and errors apart

Programs start with three streams: standard input, output and error, on file descriptors 0, 1 and 2. Errors go to the third.

See the numbers

DECODE A STATUS

What did the
command say?

Pick an exit status to see what Bash means by it. The bar shows how far things went wrong.

Status check

Choose a status
0Success

For the shell's purposes, a command that exits with a zero status has succeeded.

2 from a builtinUsed wrongly

All Bash builtins return 2 to indicate incorrect usage, generally invalid options or missing arguments.

126Not executable

The command was found but is not executable.

127Not found

The command was not found. The child process created to run it returns 127.

137Killed by signal 9

When a command ends on a fatal signal N, Bash uses 128 + N. Signal 9 is SIGKILL, which cannot be caught, blocked or ignored.

Sources: GNU Bash Reference Manual, 3.7.5 Exit Status; signal(7) from the Linux man-pages project.

Based on the GNU Bash Reference Manual and the Linux man-pages project. Signal numbers shown are those for x86 and ARM.

FOUR HABITS

Let Linux
tell you why.

These follow from how the shell and the C library report problems.

A small single-board computer with cables plugged in, on a workbench beside a notebook and a mug
AI-generated image, not a photograph of a real person or place.
  1. Check $? straight away

    It holds the status of the last command only, so the next command replaces it.

  2. Read 126 and 127 differently

    127 means the shell could not find the command; 126 means it found it but could not run it.

  3. Match the message to the name

    "No such file or directory" is ENOENT; "Permission denied" is EACCES. The names are standard; the numbers vary between systems.

  4. Remember errors use stream 2

    Standard error is separate from standard output, so redirecting output to a file can still leave error messages on screen.

TWO NUMBERS WORTH KNOWING

One byte of news,
one magic offset.

One figure on the range of answers,
one on how signals are reported.

0–255

Possible exit statuses

Exit statuses fall between 0 and 255, though the shell uses values above 125 specially. Builtins and compound commands are limited to the same range.

RANGE · GNU BASH MANUAL

128 + N

Added to a fatal signal's number

A command ended by signal N reports 128 + N. On x86 and ARM, SIGINT (interrupt from keyboard) is 2, so it shows as 130, and SIGTERM is 15, shown as 143.

SIGNALS · GNU BASH MANUAL

The exit status of the last command is kept in the special parameter $?, so you can check it straight after a command finishes.

WHY ZERO IS SUCCESS

One way to win,
many ways to fail.

The Bash manual calls the scheme seemingly counter-intuitive, but explains it: with zero as success there is one well-defined way to say a command worked, and a whole range of non-zero values for the different ways it can fail.

Error names work the same way below the shell. The man-pages project notes that errno is only meaningful when a call has signalled an error, that it is never set to zero by a system call, and that the numbers behind names like ENOSPC differ across UNIX systems.

Hands resting on a keyboard in front of a monitor, seen from behind the shoulder, with soft blue screen light
AI-generated image, not a photograph of a real person or place.

Questions

Questions people ask

What is an exit status in Linux?

The number a command returns when it finishes. The GNU Bash manual says it falls between 0 and 255, and that zero means success while any non-zero value means failure.

What does exit code 127 mean?

The command was not found. If the command is found but is not executable, the status is 126 instead.

Why do I see 130 or 137?

Bash reports a command ended by fatal signal N as 128 + N. On x86 and ARM, signal 2 (interrupt from keyboard) gives 130 and signal 9 (SIGKILL) gives 137.

What is ENOENT?

The standard error name for No such file or directory. System calls set errno to such a value when something goes wrong, and programs turn it into a readable message.

Why do error messages still appear when I redirect output?

Programs start with separate standard output and standard error streams on file descriptors 1 and 2. Redirecting one does not redirect the other.

Where the facts come from

Updated

  1. GNU Bash Reference Manual: 3.7.5 Exit Status
  2. Linux man-pages: errno(3)
  3. Linux man-pages: signal(7)
  4. Linux man-pages: stdin(3), standard I/O streams

ABOUT THIS NAME

Interested in leasing linuxspeaks.com?

linuxspeaks.com is part of the DoodleSale portfolio. It isn't in an auction right now, but you can ask about leasing it.

Ask about this name