grep: search text, with the options worth knowing
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.
TE-18-A: grep exit status: 0 match, 1 no match, 2 error
Setup for this run, inside a throwaway directory (mktemp -d):
printf 'alpha\nbeta\n' > f.txtCommand:
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=2TE-18-B: grep reads stdin when no file is given
Setup for this run, inside a throwaway directory (mktemp -d):
printf 'alpha\n' > f.txtCommand:
echo count=$(cat f.txt | grep -c alpha)Recorded output:
count=1TE-18-C: grep counts, ignores case and searches a tree; -v inverts the match
Setup for this run, inside a throwaway directory (mktemp -d):
printf 'Error one\nok\nerror two\n' > a.txt; mkdir -p sub; printf 'error three\n' > sub/b.txtCommand:
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=1TE-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):
printf 'alpha\nbeta\n' > f.txtCommand:
grep -c zzz f.txtRecorded output:
0Exit 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 errorTE-18-B: grep reads stdin when no file is givenTE-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.
All full guides: All guides