Contents

By Aaron Belchamber · 09/27/2026

grep exit status: 0, 1 and 2 explained

grep exits 0 when it selected at least one line, 1 when it selected none, and 2 when something went wrong, such as a missing file or an invalid pattern. One exception: with -q, a match exits 0 even if another file couldn't be read.

Exit status Meaning
0 At least one line selected (with -v, at least one line that didn't match)
1 No lines selected. Not an error
2 An error: unreadable file, bad regex, bad option

POSIX grep's exit status section defines 0 and 1 and says only that errors are greater than 1. GNU grep uses 2, and its --help sums it up in one line: "Exit status is 0 if any line is selected, 1 otherwise; if any error occurs and -q is not given, the exit status is 2."

Checking it

grep -q FAILED out.txt;  echo $?    # 0 if out.txt contains FAILED
grep -q NOPE out.txt;    echo $?    # 1
grep -q x missing.txt;   echo $?    # 2, with an error on stderr

I ran each line in this post with GNU grep in Git Bash and got the statuses shown.

What each common form returns

Run on GNU grep 3.0 (Git Bash on Windows 11), with a.txt holding the lines a and b, an empty file empty.txt, and missing.txt not existing:

Command Exit Why
grep a a.txt 0 a line matched
grep z a.txt 1 nothing matched
grep -v z a.txt 0 lines that don't match z were selected
grep -v -e a -e b a.txt 1 every line matched, so none was selected
grep -c z a.txt 1 prints 0, still exits 1
grep a missing.txt 2 file can't be read
grep -s a missing.txt 2 -s hides the message, not the status
grep a a.txt missing.txt 2 a match plus an error is still 2
grep -q a a.txt missing.txt 0 -q lets the match win
grep "[" a.txt 2 invalid pattern
grep --bogus a a.txt 2 unknown option
grep '' a.txt 0 the empty pattern matches every line
grep '' empty.txt 1 an empty file has no lines to select
grep -f empty.txt a.txt 1 a pattern file with no patterns matches nothing
grep -f empty.txt -v a.txt 0 so -v selects every line
echo hi | grep z 1 the same rules apply to stdin

Two of these surprise people. -s is for suppressing messages in scripts, and it leaves the 2 in place. And grep -f with an empty pattern file selects nothing, not everything.

-L reports the opposite way round

-L lists files with no match. On 3.0 here, grep -L a a.txt printed nothing and exited 0, and grep -L z a.txt printed the name and exited 1. That is the reverse of the rule above, where 0 means something was selected. I only ran 3.0 and did not check other versions, so test -L on the grep you ship with before a script depends on its status.

Three traps

1 is not an error, but set -e treats it as one

Under set -e, a grep that finds nothing stops the script. If no match is fine but a real error isn't, allow exactly 1:

grep pattern file.txt || [ $? -eq 1 ]

That passes on 0 or 1 and still fails when grep exits 2. grep -c is caught the same way: it prints 0 and exits 1.

In a pipeline you get grep's status, not the command before it

./run-tests | grep FAILED reports grep's status. That's how a red build can pass: python -m unittest ... | grep -E "^Ran |^OK|^FAILED" exits 0 when the suite fails and prints a FAILED line, because grep matched it. The rule is to gate on the runner's exit code (or set -o pipefail), never on what a search happened to find. With set -o pipefail, the pipeline returns the last non-zero status in it. Bash has it, and POSIX added it in the 2024 edition (POSIX Pipelines). In bash, ${PIPESTATUS[0]} holds the first command's status too.

-q hides an error once something matched

grep -q FAILED a.txt missing.txt exits 0 when a.txt matches, even though missing.txt couldn't be read. Drop -q when you need to know every file was searched.

How to verify

Run your command, then echo $? straight away, before anything else overwrites it. In a script, test the status directly:

if grep -q FAILED out.txt; then
  echo "tests failed"
  exit 1
fi

Questions this raises

Why does grep return exit code 1?

It selected no lines. That's a normal result, not an error; errors are 2 in GNU grep.

Does grep -c return 0 when the count is zero?

No. It prints 0 and exits 1, because no lines were selected. Scripts running under set -e stop there.

How do I get the exit status of the command before grep in a pipeline?

Use set -o pipefail so the pipeline fails when any command in it fails, or read ${PIPESTATUS[0]} in bash right after the pipeline.

What does grep exit status 2 mean?

An error: a file that couldn't be read, an invalid regular expression or a bad option. With -q, a match elsewhere still turns it into 0.

A first draft of this post was written with AI. Aaron Belchamber edited it and checked the commands, code and version numbers before publishing.