Build the grep line first
The Grep Pattern Builder builds the command. This page is about what its exit code does to the script around it.
The script ran clean every day while the log had errors in it. Then a quiet day came, the log had no ERROR lines, and the script stopped halfway: no message, no stack trace, exit status 1. The line it died on was a count. grep counted zero, printed 0, and exited 1, which is how grep says "no match". set -e read that as a failure.
Why Does grep Exit 1 Kill a set -euo pipefail Script?
Reproduced here on 2026-11-03 (Kali 2026.3, GNU bash 5.3.15, GNU grep 3.12). app.log has two INFO lines and no ERROR:
No output at all. Neither echo ran. grep's three exit codes, from the same file:
bash -x shows where it stopped:
That is the surprise: count=0 was assigned. The count is correct. An assignment from $(...) takes the exit status of the command inside it, so the line returns 1, and set -e exits right after the variable is set. grep has nothing to complain about, so nothing reaches stderr.
Does the Same Thing Happen with grep | wc -l?
Yes, because of pipefail:
wc -l prints 0 and exits 0, but pipefail makes the pipeline return the last non-zero status, which is grep's 1. Drop pipefail (set -eu) and the same script prints ✓ errors: 0 and exits 0. That is why this often shows up after someone adds -o pipefail to an old script.
Every Form, Run Against a Clean File and a Missing File
Each line below ran inside a set -euo pipefail script, once with app.log (no match) and once with missing.log (does not exist):
| Form | No match (app.log) | Missing file (missing.log) |
|---|---|---|
count=$(grep -c ERROR "$log") | dies, exit 1, no message | dies, exit 2, grep error |
count=$(grep ERROR "$log" | wc -l) | dies, exit 1, no message | dies, exit 1, grep error |
count=$(grep -c ERROR "$log" || true) | count=0, exit 0 | count='', carries on, exit 0 |
count=$(grep -c ERROR "$log" || :) | count=0, exit 0 | count='', carries on, exit 0 |
count=$(grep -c ERROR "$log" || [[ $? -eq 1 ]]) | count=0, exit 0 | dies, exit 1, grep error |
count=$(grep -c ERROR "$log") || rc=$? + check | count=0, exit 0 | dies, exit 2, grep error + own message |
if grep -q ERROR "$log" | no errors, exit 0 | no errors, exit 0, grep error on stderr |
local count=$(grep -c ERROR "$1") | errors: 0, exit 0 | hidden: local returns 0 (SC2155) |
The raw run for the two || true / || : rows:
|| : is the same as || true (: is the no-op builtin that returns 0). Both keep grep -c's 0 on a clean file, which is why they look right in testing. On a missing file the count is empty, not 0, and the script keeps going with a value that is not a number.
Which Fix Keeps a Missing File Fatal?
|| [[ $? -eq 1 ]] accepts exit 1 and fails on anything else:
It does stop the script. But the exit status is 1, not 2: the last command that ran was [[ 2 -eq 1 ]], which is false, and false is 1. A caller that reads the exit code (cron wrapper, CI step, systemd) sees "no match", not "error". If the code matters, capture it:
|| rc=$? puts the assignment on the left of ||, so set -e never fires, and $? still holds grep's own status when rc is set. For the pipeline form, wrap the grep: count=$( { grep ERROR "$log" || [[ $? -eq 1 ]]; } | wc -l). That prints errors: '0' on a clean file and stops with exit 1 on a missing one.
Is if grep -q Safe?
From set -e, yes. A command used as an if condition never triggers it:
From a missing file, no. if only knows true (0) and false (anything else), so exit 2 takes the else branch:
The script reports a clean log for a log that does not exist. When the file can be absent, branch on the code:
How Does ShellCheck Help?
ShellCheck 0.11.0 does not flag count=$(grep -c ERROR app.log): the line is valid, the problem is the exit code at run time. In a file with all three forms, line 3 passes and the two neighbours are caught:
SC2155 is the reason local found=$(grep ...) "works": local returns 0 and hides grep's status, both the harmless 1 and the real 2. Split it into local found and found=$(grep ...), then handle the code with one of the fixes above.
Tested On
Kali 2026.3 (bash 5.3.15, GNU grep 3.12), and the same scripts in ubuntu:24.04 (bash 5.2.21, GNU grep 3.11) and ubuntu:26.04 (bash 5.3.9, GNU grep 3.12) containers. Same exit codes in all three: count.sh exits 1, || [[ $? -eq 1 ]] exits 1 on a missing file, if grep -q prints no errors for a missing file, and the rc capture exits 2.
Frequently Asked Questions
Why does my bash script exit when grep finds nothing?
grep returns exit status 1 when no line matches. set -e stops the script on any non-zero status from a command that is not tested by if, while, && or ||, and a variable assignment from $(...) takes the status of the command inside it. So count=$(grep -c ERROR app.log) assigns 0 to count and then set -e exits with status 1. Nothing is printed because grep did not fail in its own view: it found no lines and said so with its exit code.
Is grep -c ... || true safe?
It stops the no-match exit, but it also swallows exit 2. When the file is missing or unreadable, grep prints its error on stderr, || true turns the status into 0, and the variable is empty instead of 0. Any arithmetic or comparison after that runs on an empty string. Use || [[ $? -eq 1 ]] or capture the status with || rc=$? if a missing file must stop the script.
Why does grep -c print 0 but still fail?
grep -c prints the count even when the count is zero, and still exits 1 because nothing matched. The 0 is captured into the variable before set -e acts on the status, which is why bash -x shows count=0 on the last line before the script exits. The count and the exit status are separate outputs: the count goes to stdout, the match result goes to $?.
Why does the same grep line work inside a function with local?
local count=$(grep -c ERROR "$1") returns the exit status of the local builtin, which is 0, not grep's. set -e never sees the 1, so the function keeps running. It also never sees a 2, so a missing file is hidden too. ShellCheck flags this as SC2155: declare the variable with local first, then assign it on its own line so the status is visible.
Does grep -q in an if statement trigger set -e?
No. A command used as an if condition is exempt from set -e, so if grep -q ERROR app.log is safe on a no-match file. The catch is that a missing or unreadable file returns 2, which if also treats as false. The script prints grep's error on stderr and runs the else branch as if the file were clean. Capture the status with grep -q ... || rc=$? and branch on 0, 1 and anything else if the file can be absent.
The safe bash script template starts every script with set -euo pipefail, which is why each grep in it needs one of these decisions, and The Production Bash Toolkit ships that template ShellCheck-clean.
Part of the bash snippets collection
Related Scripts
- Search Files for Text with grep —
grep -rn,--include,--exclude-dirand the flags behind them - Bash Error Handling — what
set -e,set -uandpipefaileach change - Bash Exit Code Lookup — what 1, 2, 126, 127 and the signal codes mean
- Fix "unary operator expected" — what an empty count does when it reaches
[ ] - Bash Error Messages, Decoded — nine errors, each with the one check that names the cause