Skip to content

grep Exit Code 1 Under set -euo pipefail: Why the Script Dies Silently

greperror-handlingexit-codesset-etroubleshooting
7 min read
Matching toolgrep Pattern Builder

Quick Answer

grep exit code 1 means no lines matched; it is not an error, but set -e treats it as one. Under set -euo pipefail, count=$(grep -c ERROR app.log) on a file with no ERROR lines assigns count=0 and then stops the script with exit status 1, printing nothing. grep uses three codes: 0 = a line matched, 1 = no match, 2 = an error such as grep: missing.log: No such file or directory. The fix is to accept 1 and keep 2 fatal. grep ... || true and || : accept both, so a missing file gives an empty count and the script carries on. grep ... || [[ $? -eq 1 ]] stops on a missing file, but with exit 1, not 2. To keep the real code, capture it: count=$(grep -c ERROR "$log") || rc=$?, then exit when rc is above 1. if grep -q is safe from set -e, but it takes the no-match branch on a missing file.

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:

bash
#!/bin/bash # Script: count.sh # Purpose: Reproduce a set -e script dying silently when grep finds no match # Usage: ./count.sh set -euo pipefail CHECK="✓" CROSS="✗" LOG_FILE="app.log" count=$(grep -c ERROR "$LOG_FILE") if (( count > 0 )); then echo "$CROSS errors: $count"; else echo "$CHECK errors: $count"; fi
text
$ bash count.sh exit=1

No output at all. Neither echo ran. grep's three exit codes, from the same file:

text
$ grep -c INFO app.log 2 exit=0 $ grep -c ERROR app.log 0 exit=1 $ grep -c ERROR missing.log grep: missing.log: No such file or directory exit=2

bash -x shows where it stopped:

text
$ bash -x count.sh + set -euo pipefail + CHECK=✓ + CROSS=✗ + LOG_FILE=app.log ++ grep -c ERROR app.log + count=0 exit=1

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:

bash
#!/bin/bash # Script: pipe.sh # Purpose: Reproduce pipefail stopping a script when grep in a pipeline finds no match # Usage: ./pipe.sh set -euo pipefail CHECK="✓" CROSS="✗" LOG_FILE="app.log" # shellcheck disable=SC2126 # grep | wc -l on purpose: it is the form that breaks count=$(grep ERROR "$LOG_FILE" | wc -l) if (( count > 0 )); then echo "$CROSS errors: $count"; else echo "$CHECK errors: $count"; fi
text
$ bash pipe.sh exit=1

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):

FormNo match (app.log)Missing file (missing.log)
count=$(grep -c ERROR "$log")dies, exit 1, no messagedies, exit 2, grep error
count=$(grep ERROR "$log" | wc -l)dies, exit 1, no messagedies, exit 1, grep error
count=$(grep -c ERROR "$log" || true)count=0, exit 0count='', carries on, exit 0
count=$(grep -c ERROR "$log" || :)count=0, exit 0count='', carries on, exit 0
count=$(grep -c ERROR "$log" || [[ $? -eq 1 ]])count=0, exit 0dies, exit 1, grep error
count=$(grep -c ERROR "$log") || rc=$? + checkcount=0, exit 0dies, exit 2, grep error + own message
if grep -q ERROR "$log"no errors, exit 0no errors, exit 0, grep error on stderr
local count=$(grep -c ERROR "$1")errors: 0, exit 0hidden: local returns 0 (SC2155)

The raw run for the two || true / || : rows:

text
$ bash fix-true.sh app.log errors: '0' exit=0 $ bash fix-true.sh missing.log grep: missing.log: No such file or directory errors: '' exit=0 $ bash fix-colon.sh missing.log grep: missing.log: No such file or directory errors: '' exit=0

|| : 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:

text
$ bash fix-exit1.sh app.log errors: '0' exit=0 $ bash fix-exit1.sh missing.log grep: missing.log: No such file or directory exit=1

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:

bash
#!/bin/bash # Script: fix-rc.sh # Purpose: Count ERROR lines without dying on no match or hiding a missing log # Usage: ./fix-rc.sh [LOG_FILE] (default: app.log) set -euo pipefail CHECK="✓" CROSS="✗" LOG_FILE="${1:-app.log}" GREP_NO_MATCH=1 # grep's "nothing matched"; 2 and above are real errors rc=0 # The assignment sits left of ||, so set -e never fires and $? is still grep's code count=$(grep -c ERROR "$LOG_FILE") || rc=$? if (( rc > GREP_NO_MATCH )); then echo "$CROSS grep failed on $LOG_FILE (exit $rc)" >&2 exit "$rc" fi echo "$CHECK errors: $count"
text
$ bash fix-rc.sh app.log ✓ errors: 0 exit=0 $ bash fix-rc.sh missing.log grep: missing.log: No such file or directory ✗ grep failed on missing.log (exit 2) exit=2

|| 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:

text
$ bash ifq.sh no errors still running exit=0

From a missing file, no. if only knows true (0) and false (anything else), so exit 2 takes the else branch:

text
$ bash ifq-missing.sh missing.log grep: missing.log: No such file or directory no errors exit=0

The script reports a clean log for a log that does not exist. When the file can be absent, branch on the code:

bash
rc=0 grep -q ERROR "$log" || rc=$? case $rc in 0) echo "errors found" ;; 1) echo "no errors" ;; *) echo "cannot read $log" >&2; exit "$rc" ;; esac
text
$ bash ifq-rc.sh missing.log grep: missing.log: No such file or directory cannot read missing.log exit=2

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:

text
$ shellcheck check.sh In check.sh line 4: total=$(grep ERROR app.log | wc -l) ^----------------^ SC2126 (style): Consider using 'grep -c' instead of 'grep|wc -l'. In check.sh line 6: local found=$(grep -c ERROR "$1") ^---^ SC2155 (warning): Declare and assign separately to avoid masking return values.

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

PAID RESOURCE — $9

The Production Bash Toolkit

An operational script system + a 30-function shared library + a 52-page field guide. The production layer the free snippets don't cover.

Get the Toolkit →
curl -O bashlib-starter.sh

Get the bashlib starter

Ten functions I source into every script on my own boxes — strict-mode setup, an ERR trap that names the failing line, lock and timeout wrappers, and cleanup that runs on every exit path. After that, a short email only when something breaks on a real box and I've worked out why, about twice a month. One-click unsubscribe.

BashSnippets logo

Written by Travis

Creator of BashSnippets.xyz

bashsnippets.xyz/about

Related Snippets

Frequently Asked Questions

faq — snippet

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.

faq — snippet

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.

faq — snippet

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 $?.

faq — snippet

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.

faq — snippet

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.