$_ CLI In Depth
Learning progress 0%
lesson terminal shell kernel

Exit Status: How Programs Report Success

5 min read Module 1: Terminal, Shell & Kernel Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:27

Every process returns one number

When a program ends, it calls exit(3), or the lower-level _exit(2) system call, with a status number. exit(3) runs cleanup handlers and flushes the standard I/O buffers before it makes the system call, while _exit(2) ends the process at once. The parent process collects the number with wait4, which is the same collection step you saw in the system call lesson. Only the low eight bits survive. The status is therefore a value from 0 to 255.

You can see this with the shell. Bash exposes the last status as $?:

BASH1 lines
ls /nope; echo "status=$?"

ls prints its error message to standard error, then exits with 2, and echo prints status=2. Read $? immediately after the command you care about. Any other command, including another echo, replaces it.

Conventions for the numbers

There is no universal meaning for each number. The program decides, and the man page or source documents the choice. The widely followed conventions are:

StatusMeaning
0Success
1General failure; for grep and diff it means "no match" or "files differ"
2Misuse or serious error; ls and grep use it for real errors such as an unreadable file
126Found but not executable, such as a directory or a file without the execute bit
127Command not found
128+NKilled by signal N; for example 130 is Ctrl-C (SIGINT, signal 2) and 143 is SIGTERM (15)

Try the two failure cases the shell itself produces:

BASH2 lines
nosuchcommand; echo $?
/etc; echo $?

The first line prints an error that the command is not found, then 127. The second prints that /etc is a directory and cannot be run, then 126. Both messages come from bash, not from a program.

true, false, and negation

true and false are commands that do nothing but exit with 0 and 1. They are useful for testing the shell itself:

BASH3 lines
false; echo $?
true; echo $?
! true; echo $?

The output is 1, 0, and 1. The ! operator inverts a pipeline's status: it turns 0 into 1 and any nonzero status into 0. It is a shell keyword, not a command.

Chaining with && and ||

The && operator runs the right side only if the left side returned 0. The || operator runs the right side only if the left side returned nonzero. Both operators are evaluated left to right, with equal precedence:

BASH2 lines
false && echo yes || echo no
true && echo yes || echo no

The first line prints no: false fails, so echo yes is skipped, and the || branch runs. The second line prints yes, because true succeeds and the || branch is skipped. This left-to-right reading is why a && b || c is not the same as "a and then b or c" in every case. When b itself fails, c runs.

if runs a command, not a boolean

The if keyword does not evaluate a condition the way a programming language does. It runs a command and branches on its exit status. Any command works, and the most useful ones are quiet filters that report through their status. grep -q is the standard example: it prints nothing and exits 0 if the pattern matches, 1 if it does not, and 2 on an error.

BASH1 lines
if echo "hello world" | grep -q hello; then echo found; fi

The output is found. The pipeline's status is the status of its last command, which is grep here. [ and [[ work the same way: they are commands or keywords that return a status, and the if does not care how the status was produced.

Pipelines and their status

In a pipeline such as a | b | c, the status is that of the last command, c. The earlier commands can fail without changing the result. Bash keeps the status of each command in the array PIPESTATUS, which is overwritten by the next pipeline:

BASH2 lines
false | true; echo $?
false | true; echo ${PIPESTATUS[@]}

The first line prints 0, because true was last. The second prints 1 0, the status of each stage. Each line reads the status right after its own pipeline. Any later command, including a separate echo $?, overwrites both $? and PIPESTATUS, so capture them immediately if you need them.

To make a pipeline fail when any stage fails, enable the pipefail option. It is off by default. Turn it on, run the check, then turn it off again so the change does not leak into later work:

BASH1 lines
set -o pipefail; false | true; echo $?; set +o pipefail

The output is 1. With pipefail, the pipeline's status is the rightmost nonzero status, or 0 if every stage succeeded. This option is common in scripts, and it is also a useful habit to know when a long pipe reports success after a stage has failed.

Signals and Ctrl-C

A process that dies from a signal has no exit call to give a status, so the shell reports 128 plus the signal number. Bash does not hide this. Send a process a SIGTERM from inside a shell to see it:

BASH1 lines
bash -c 'kill -TERM $$'; echo $?

The output is 143, which is 128 plus 15. If you run sleep 100 at the prompt and press Ctrl-C, the status is 130, because Ctrl-C sends SIGINT, signal 2. To see it yourself, run sleep 100, press Ctrl-C, and then run echo $? at the next prompt. The same number appears for any command that you stop this way.

Tip

When a command fails with 2, read its message before you retry. Exit 2 usually means the tool was given something it could not use, while 1 often just means "no match" for a search tool. Treating both the same hides the difference.

Exit status, && and ||

Visualizer

Every command ends with a number from 0 to 255. Zero means success; everything else is a kind of failure, and &&, || and if branch on it.

1 / 6
$ true; echo $?
0
status
0
meaning
success

true does nothing and exits 0. Zero is the only success value.

true; echo $?; false; echo $?; ls /nope; echo $?; nosuchcmd; echo $?
Terminal lab · 4 steps · real Linux in your browser

Read and produce exit statuses

Capture $?, see 127 and PIPESTATUS, and write a script whose exit status means something.

01
Save a failure status
Run ls /nope, then immediately save its exit status: ``bash ls /nope; echo $? > status.txt ``
Show hint for step 1
`$?` holds the status of the most recent command, so nothing can run in between.
02
Command not found
Run a command that does not exist, such as nosuchcmd, and save its exit status into notfound.txt.
Show hint for step 2
`nosuchcmd; echo $? > notfound.txt`
03
Every status in a pipeline
$? only reports the last command of a pipeline. Save all of them: ``bash false | true; echo "${PIPESTATUS[@]}" > pipestatus.txt ``
Show hint for step 3
PIPESTATUS is an array with one entry per pipeline stage.
04
A script that reports
Write isfile.sh so that bash isfile.sh PATH exits 0 when PATH is a regular file and 1 otherwise. It doesn't need to print anything.
Show hint for step 4
A script's status is that of its last command: `[ -f "$1" ]` is enough.
Check yourself Question 1 of 4
Concept Check Status for a missing command recall

What exit status does bash report for a command name it cannot find on the PATH?

Recap & Key Takeaways

Exit status is an integer from 0 to 255, where 0 means success; read $? immediately because the next command replaces it.
Common conventions are 1 for general failure, 2 for misuse or errors, 126 for not executable, 127 for not found, and 128+N for death by signal N, such as 130 for Ctrl-C.
if, &&, and || branch on a command's exit status; grep -q is the standard quiet test. A pipeline reports only its last command unless set -o pipefail is on.
PIPESTATUS holds each stage's status for the most recent pipeline, and it is overwritten by the next command.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue