Build the grep line first
The Grep Pattern Builder builds the command and its flags. This page is about the one case where a correct grep prints nothing.
The health check greps the log for job ok and stores the last match. One night the variable came back empty, the exit status was 0, and the check reported nothing useful. The log was fine except for one line where a worker wrote a NUL byte. One byte in one line made grep call the whole file binary, and grep's response to a binary match is to say so on stderr and print none of the lines.
What Does "binary file matches" Mean?
Reproduced here on 2026-11-03 (Kali 2026.3, GNU bash 5.3.15, GNU grep 3.12, LANG=en_US.UTF-8). app.log has three lines; the middle one holds a NUL:
Two lines match. grep printed neither, wrote one line to stderr, and exited 0. With stderr hidden there are zero bytes of output. Note that the matching lines are clean: the NUL is in the line that does not match.
Why Does line=$(grep ...) Succeed with an Empty Value?
That is the surprise: it is not an error. Exit 0 means "found a match", which is true. $(...) captures stdout only, and stdout is empty. set -euo pipefail has nothing to catch, because no command failed, as this script shows:
Under cron, stderr usually goes to a mail spool or nowhere, so the only trace is the empty value. grep -c ok app.log (prints 2), grep -q and grep -l all work normally on the same file; only the commands that print lines are affected.
Did Older grep Behave Differently?
Yes, and the change breaks code in the other direction. The same file in an ubuntu:20.04 container (GNU grep 3.4, bash 5.0.17):
grep 3.4 wrote Binary file app.log matches to stdout, so the message itself was captured as the matching line. GNU grep 3.5 moved it to stderr and changed the wording. ubuntu:24.04 (grep 3.11) and ubuntu:26.04 (grep 3.12) both behave like Kali: stderr, empty $line, exit 0. A script that parsed the old output on a 20.04 host gets an empty string after the upgrade.
How Do I Make grep Print the Lines?
-a or its long form --binary-files=text:
For a script, put -a in the capture and check for an empty result anyway:
-a prints a matching line with its bad bytes intact, which is why the script strips NULs before printing. Another route is to clean the stream first: tr -d '\000' < app.log | grep ok printed both lines and exited 0.
When Should I Use grep -I Instead?
When the binary files are real binaries you want skipped, not logs you want read. -I (--binary-files=without-match) treats a binary file as having no match:
On one file, -I is silent with exit 1: the log's two job ok lines are now reported as "no match". Across a tree with -r, it drops the noise from images and compiled files. Do not use it on logs.
How Do I Find the Byte That Caused It?
The obvious check gives the wrong answer:
Without -a, grep handles the file as binary and treats NUL as a line terminator, so no line ever contains a NUL and the count is 0, exit 1: "no NUL here". Add -a. -n gives the line number and cat -v shows the NUL as ^@.
Can a Non-NUL Byte Trigger It?
Yes, and only in a UTF-8 locale. latin1.log is the same log with café written as a single Latin-1 byte (0xE9) and no NUL:
Unlike the NUL case, the invalid byte only matters on a line grep would print: ok lines come through, the restart line does not. LC_ALL=C makes every byte valid, so the line prints. It does nothing for a NUL. In the Ubuntu containers, which have no LANG set and run in the C locale, grep restart latin1.log printed the line with no message, while LC_ALL=C.UTF-8 brought the message back. That is how a grep can work in a cron job or container and fail at an interactive shell, or the reverse. To list the lines with invalid bytes, stay in the UTF-8 locale and ask for lines that are not entirely valid characters:
In the C locale every byte matches ., so the same command finds nothing. It also finds nothing in app.log: a NUL is a valid character, which is why the NUL check above needs its own pattern.
Tested On
Kali 2026.3 (bash 5.3.15, GNU grep 3.12, en_US.UTF-8); ubuntu:24.04 (bash 5.2.21, GNU grep 3.11) and ubuntu:26.04 (bash 5.3.9, GNU grep 3.12) containers, C locale: same stderr message, exit 0 and empty $line, -I exit 1, grep -c -a -P '\x00' prints 1. ubuntu:20.04 (bash 5.0.17, GNU grep 3.4): old stdout message, captured into $line.
Frequently Asked Questions
Why does grep say binary file matches on a text log?
Because one byte in it is not text. In the runs here, a NUL byte anywhere in the file, or a byte that is invalid in your locale's encoding on a line it would print, marks the file binary. Logs pick these up from a crashed writer, a preallocated or truncated file, a program that logs raw binary fields, or a Latin-1 character in a UTF-8 locale. The other lines are still plain text: grep -a prints them.
Is binary file matches an error?
No. grep exits 0, the same as a normal match, because it did find the pattern. The message is written to stderr, not stdout, since GNU grep 3.5. That is the trap: a script that captures stdout gets an empty string, sees success, and has nothing to tell it the data was withheld. Check for an empty result, or use -a so the lines come through.
What is the difference between grep -a and grep -I?
-a (--binary-files=text) treats every file as text and prints the matching lines, NUL bytes included. -I (--binary-files=without-match) treats binary files as having no match at all: nothing printed, no message, and exit 1 for a single file. Use -a when the file is a log with a stray bad byte and you want its lines. Use -I with grep -r over a tree that holds real binaries such as images or compiled files you want to skip.
Why does grep -c -P '\x00' say 0 when the file has a NUL?
Without -a, grep handles a binary file differently: GNU grep may treat NUL bytes as line terminators, so no line ever contains one and the pattern cannot match. Tested here: grep -c -P '\x00' app.log printed 0 and exited 1 on a file with one NUL, while grep -c -a -P '\x00' app.log printed 1. Add -a, and -n to get the line number, when searching for the byte itself.
Does LC_ALL=C fix binary file matches?
Only when the cause is an encoding error. With LANG=en_US.UTF-8, a line containing a lone 0xE9 byte made grep report binary file matches; LC_ALL=C grep printed the line, because in the C locale every byte is valid. A NUL byte is binary in every locale: LC_ALL=C grep ok app.log still printed the message. Cron and containers often run in the C locale, which is why the same grep can behave differently there.
The safe bash script template checks captured values before using them, which is the check that catches this one, 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 -euo pipefailcatches, and why it cannot catch an exit 0 - Bash Read File Line by Line — reading a log in a loop when grep is not enough
- Bash Exit Code Lookup — what 0, 1 and 2 mean for grep and other commands
- Bash Error Messages, Decoded — nine errors, each with the one check that names the cause