# grep: search text, with the options worth knowing

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/grep-basics-full-en
grep prints matching lines; exit 0 means a match, 1 no match, 2 an error. No-match is a normal result; reads stdin when no file is given.

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: 4 tests were run, TE-18-A, TE-18-B, TE-18-C and TE-18-D. The expected non-zero exit is TE-18-D (exit status 1). 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-18-A: grep exit status: 0 match, 1 no match, 2 error

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

```bash
printf 'alpha\nbeta\n' > f.txt
```

Command:

```bash
grep -q alpha f.txt; echo match=$?; grep -q zeta f.txt; echo nomatch=$?; grep -q alpha missing.txt 2>/dev/null; echo error=$?
```

Recorded output:

```
match=0
nomatch=1
error=2
```

### TE-18-B: grep reads stdin when no file is given

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

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

Command:

```bash
echo count=$(cat f.txt | grep -c alpha)
```

Recorded output:

```
count=1
```

### TE-18-C: grep counts, ignores case and searches a tree; -v inverts the match

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

```bash
printf 'Error one\nok\nerror two\n' > a.txt; mkdir -p sub; printf 'error three\n' > sub/b.txt
```

Command:

```bash
echo count=$(grep -c -i error a.txt); echo tree=$(grep -r -c -i error . | sort | tr '\n' ' '); echo inverted=$(grep -v -i error a.txt | wc -l)
```

Recorded output:

```
count=2
tree=./a.txt:2 ./sub/b.txt:1
inverted=1
```

### TE-18-D: expected non-zero exit: grep -c prints 0 and exits 1 when nothing matches

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

```bash
printf 'alpha\nbeta\n' > f.txt
```

Command:

```bash
grep -c zzz f.txt
```

Recorded output:

```
0
```

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

## Reading the recorded output

In TE-18-A the three numbers are the exit statuses of three grep -q runs. The search for alpha in f.txt gave 0, the search for zeta in the same file gave 1, and the search in missing.txt, a file that does not exist, gave 2 with its error message hidden by 2>/dev/null. TE-18-B counted one match when the file was piped in through cat, so grep read standard input. In TE-18-C, a.txt has two lines that contain error in either case, so -c -i printed 2. The recursive count lists a.txt with 2 and sub/b.txt with 1. The last value, inverted=1, means one line is left after -v -i error; by the setup that is the line ok.

TE-18-D: by the setup, f.txt holds the lines alpha and beta. grep -c zzz f.txt printed 0 and exited with status 1, so a count of 0 comes with the no-match status.

## Limits

No-match is a normal result; reads stdin when no file is given.

## Source

- https://www.gnu.org/software/grep/manual/html_node/Exit-Status.html
- https://www.gnu.org/software/grep/manual/grep.html

## Evidence

- `TE-18-A`: grep exit status: 0 match, 1 no match, 2 error
- `TE-18-B`: grep reads stdin when no file is given
- `TE-18-C`: grep counts, ignores case and searches a tree; -v inverts the match

- 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-18-A` | grep exit status: 0 match, 1 no match, 2 error | 0 | PASS | `0472b74f7995` |
| `TE-18-B` | grep reads stdin when no file is given | 0 | PASS | `5a15ccde2184` |
| `TE-18-C` | grep counts, ignores case and searches a tree; -v inverts the match | 0 | PASS | `1bab6eb1ff66` |
| `TE-18-D` | expected non-zero exit: grep -c prints 0 and exits 1 when nothing matches | 1 | PASS | `f65411a94d1b` |

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
- [chmod: read the permission string and change it](https://kairos-2z6s.onrender.com/res/chmod-basics-full-en)
- [find: list files by name and sort the result](https://kairos-2z6s.onrender.com/res/find-basics-full-en)
- [sed basics: replace text in a stream](https://kairos-2z6s.onrender.com/res/sed-basics-full-en)
- [Leaked API key: revoke first, then scan history](https://kairos-2z6s.onrender.com/res/leaked-key-revoke-and-scan-en)
- [.gitignore patterns: which files Git treats as untracked](https://kairos-2z6s.onrender.com/res/gitignore-patterns-full-en)
- [Answer questions about a log file with a pipeline](https://kairos-2z6s.onrender.com/res/log-pipeline-full-en)
- [tar basics: pack, list and unpack a folder](https://kairos-2z6s.onrender.com/res/tar-basics-full-en)
