$_ CLI In Depth
Learning progress 0%
lesson scripting exit-codes control-flow

Exit Codes, && and ||

5 min read Module 13: Scripting Basics & Exit Codes Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:25

Exit status is a number

When a command finishes, it returns a status between 0 and 255. The shell stores the status of the most recent command in the special parameter $?. Zero means success. Any nonzero value means the command failed or was stopped. Different numbers can carry different meanings for a particular program. The grep program uses 1 for no match and 2 for an error, while diff uses 1 for differences. Read the manual page of a program for its own values.

BASH2 lines
grep -q root /etc/passwd
echo $?

Read $? right after the command you care about, because the next command overwrites it. Store the value in a variable if you need it later. For a pipeline, $? holds the status of the last command in the pipeline, and the array PIPESTATUS keeps the status of every stage.

Setting the status with exit

A script ends with exit N, and N becomes the status of the script. Without an argument, exit returns the status of the last command that ran. A function ends with return N, which sets the status of the call and does not stop the script. Values are reduced modulo 256, so exit 256 returns 0.

The status of a script is what the caller sees in $?, and what && and || test. A script that prints an error and exits with a nonzero status is much more useful than one that prints an error and then succeeds. The caller can react to the status without parsing text, and that is the design the rest of the shell relies on.

The standard values

A few values have conventional meanings. Zero means success. One means a general failure. Two is the usual status for misuse of a command, such as a bad option, and many scripts use it for usage errors. The value 126 means the file was found but could not be executed. The value 127 means the command name was not found. A value above 128 means the process was ended by a signal, and 128 plus N gives the number of the signal N. Ctrl-C sends SIGINT, which is signal 2, so an interrupted command reports 130.

A status of zero is the only value that counts as success in an if test or an && list. Any other value, including values that a program uses for warnings, counts as failure to the shell. If a program reports a warning with a nonzero status that you want to treat as success, you must handle that case explicitly, for example with a || branch that checks the specific value.

These values come from the shell and the kernel, not from the program. A script that wants a clear status should choose from the same conventions, so that callers can test for the situations they care about.

&& and || run based on status

The operator && runs the right side only when the left side succeeded. The operator || runs the right side only when the left side failed. Both are short-circuit operators, so the shell evaluates the right side only when it is needed. That is what makes them useful as guards.

BASH2 lines
mkdir -p backup && cp notes.txt backup/
test -d backup || echo "no backup directory"

The first line copies the file only if the directory exists or was created. The second line reports the problem only when the directory is missing. These operators express a small if statement in one line, which is fine for simple cases. For anything with several steps, use if, because a long chain becomes hard to read.

The trap in a && b || c

A chain such as a && b || c looks like an if-then-else, and it nearly is. The command c runs when a fails, but it also runs when a succeeds and b fails, because then the chain ends with a nonzero status. If b can fail, the else branch can run after a success. The example below shows the problem:

BASH1 lines
true && false || echo runs

The command prints runs, even though the first command succeeded. The middle command failed, so the || branch ran. When the middle command can fail, write the logic as an if statement, where each branch is explicit.

Negation and pipelines

The ! prefix inverts the status of a pipeline. So ! grep -q err log is true when the file has no match. The errexit option, set with set -e, does not stop a script for a command that is part of an if test, an && or || list, or a negated pipeline. That is why a script with set -e can keep running after a failed test.

A pipeline reports the status of its last command by default. A failing producer can hide behind a successful consumer, so false | cat returns 0. The command set -o pipefail makes the pipeline report the rightmost nonzero status among its stages, which exposes the failure.

Hands-on: check statuses

Run the lines below one at a time and compare each result with your prediction.

BASH3 lines
false; echo $?
true && false || echo runs
false | cat; echo $?

Each status in this unit has a practical use. A script that checks a precondition should exit with a nonzero status when the precondition fails, so that the caller can stop before doing damage. A script that reports a usage error should use 2. A script that finishes its work should exit with 0 explicitly, so that a stray failing command earlier in the file does not leak out as its result.

The output should be 1, then runs, then 0. Next, run set -o pipefail, repeat the third line, and see that the status becomes 1. Run set +o pipefail afterward to restore the default for the rest of your session.

Terminal lab · 2 steps · real Linux in your browser

Report status with exit codes

Set a script exit status explicitly, then build a guard with && and || that reports failures.

01
Exit with a chosen status
Create need.sh in /workspace. It takes one directory argument. When that directory exists, it prints ok followed by the path and exits with status 0. When it does not exist, it prints missing followed by the path to standard error and exits with status 3. Print nothing to standard output in the failure case. Test it with a directory that exists and with a path that does not exist.
Show hint for step 1
Use an if with a directory test. Call exit with the number the step asks for in each branch.
02
Chain steps and report failure
Create touchlog.sh in /workspace. It takes one file path, creates the parent directory if it is missing, and creates the file. On success it prints made followed by the path and exits with status 0. If either step fails, it prints failed followed by the path to standard error and exits with status 1. Chain the two steps with the && operator inside an if statement, so that a failure in either step takes the failure branch. Do not end the script with a bare || that hides the failure.
Show hint for step 2
Put the && chain in the condition of an if. The else branch runs when any command in the chain fails.
Check yourself Question 1 of 4
Concept Check Status of a process killed by SIGTERM recall

A command is ended by SIGTERM, which is signal 15. What does $? hold right after it?

Recap & Key Takeaways

Every command returns a status from 0 to 255, where 0 means success, and $? holds the status of the most recent command.
exit N sets the status of a script, and return N sets the status of a function without ending the script.
The && and || operators run their right side based on the status of the left side, but a && b || c also runs c when b fails.
A pipeline reports the status of its last command unless set -o pipefail is in effect.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue