# Bash exit codes: 0 is success, and && and || use it

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/bash-exit-codes-full-en
`$?` holds the exit status of the last command that ran; zero is success in shell tests, and non-zero is not success.

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

(Test: 3 tests were run, TE-37-A, TE-37-B and TE-37-C. The expected non-zero exit is TE-37-C (exit status 3). Every row has a recorded log in the evidence archive. The evidence table lists the exit status and log hash of every row, and no Exit status line on this page is for the A row.)

### TE-37-A: zero means success in shell tests, non-zero means not-success; some tools use non-zero for a normal result

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
printf 'a\n' > f.txt
```

Command:

```bash
true; echo true=$?; false; echo false=$?; grep -q z f.txt; echo grep_nomatch=$?; grep -q a f.txt && echo and_ran; grep -q z f.txt || echo or_ran
```

Recorded output:

```
true=0
false=1
grep_nomatch=1
and_ran
or_ran
```

### TE-37-B: a shell function can return its own status, and $? reads it

No setup needed: the command below creates what it uses in the throwaway folder.

Command:

```bash
f() { return 3; }; f; echo rc=$?
```

Recorded output:

```
rc=3
```

Exit status: 0. This status and the output match what was written before the run.

### TE-37-C: expected non-zero exit: a script that ends with exit 3 gives status 3 to its caller

No setup needed: the command below creates what it uses in the throwaway folder.

Command:

```bash
bash -c 'exit 3'; echo inner=$?; exit 3
```

Recorded output:

```
inner=3
```

Exit status: 3. This status and the output match what was written before the run.

## Reading the recorded output

true=0 and false=1 show the exit statuses of the commands true and false. grep_nomatch=1 shows that grep -q exits with 1 when the text z is not in f.txt. The line and_ran was printed because grep -q a f.txt found a match and exited 0, so the command after && ran. The line or_ran was printed because grep -q z f.txt exited 1, so the command after || ran.

TE-37-B: the function returned 3, and $? read rc=3 right after the call. TE-37-C: bash -c 'exit 3' gave inner=3, and the outer command then ended with exit status 3 as well.

## Limits

Some tools use a non-zero status for a normal result, such as no match or a difference.

## Source

- https://www.gnu.org/software/bash/manual/bash.html#Exit-Status

## Evidence

- `TE-37-A`: zero means success in shell tests, non-zero means not-success; some tools use non-zero for a normal result

- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.

## Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) |
|---|---|---|---|---|
| `TE-37-A` | zero means success in shell tests, non-zero means not-success; some tools use non-zero for a normal result | 0 | PASS | `0c6377a24720` |
| `TE-37-B` | a shell function can return its own status, and $? reads it | 0 | PASS | `84aad86f0bd3` |
| `TE-37-C` | expected non-zero exit: a script that ends with exit 3 gives status 3 to its caller | 3 | PASS | `4fc9bcddd3a8` |

Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.

## Related guides
- [awk basics: split records into fields](https://kairos-2z6s.onrender.com/res/awk-basics-full-en)
- [Bash variables, loops and if: the basics](https://kairos-2z6s.onrender.com/res/bash-variables-loops-full-en)
- [chmod: read the permission string and change it](https://kairos-2z6s.onrender.com/res/chmod-basics-full-en)
- [Bash conditionals: [ ] and [[ ]] with an empty variable](https://kairos-2z6s.onrender.com/res/bash-conditionals-full-en)
- [Bash script arguments and functions: $1, $@, shift and return codes](https://kairos-2z6s.onrender.com/res/bash-arguments-functions-full-en)
