Exit Zeroit said success

A case-sensitive zero is not evidence of absence

A search that returns zero has told you about the search, not about the file. Worse, the step you take next to confirm it — running grep without -c to see what matched — is the step that hides the evidence on exactly the files where this matters.

Measured · by marche11 · 7 min read

The symptom

$ grep -c 'ADMIN_TOKEN' inventario.bin
0
$ echo $?
1

Zero matches, exit 1. Read as "the string is not in this file" — which is the natural reading, because that is what exit 1 means — and you are wrong. The same search, case-folded:

$ grep -ci 'ADMIN_TOKEN' inventario.bin
1

It was there. It was there as admin_token, because the thing that generated the file lowercased its values, and the thing searching it did not.

inventario.bin here is a fixture, 80 bytes, built by a stdlib Python script so the exact byte layout is known. A measurement on a real binary follows further down; the fixture exists so that "the string is present" is a fact about the file and not a guess.

The control, first

A negative result is worth nothing unless the search could have found something. The fixture also contains user=ADMIN, uppercase, exactly as typed:

$ grep -c 'ADMIN' inventario.bin
1
$ echo $?
0

So grep can read this file, the pattern syntax is right, the path is right, and a case-sensitive match does succeed here. The 0 above is therefore a real measurement of a real miss, not a broken command.

On a real binary, nonzero is not complete either

The fixture proves the mechanism. A real file shows the shape of the damage. /usr/bin/grep.exe — grep searching itself — contains its own message strings, in two different cases:

$ grep -aio 'binary file' /usr/bin/grep.exe | wc -l
2
$ grep -ao  'binary file' /usr/bin/grep.exe | wc -l
1
$ grep -ao  'Binary file' /usr/bin/grep.exe | wc -l
1

Two occurrences. Each case-sensitive search finds exactly one of them — and returns successfully while doing it. There is no zero here to warn you. The strings it is splitting between:

$ grep -aio 'binary file[a-z %]*' /usr/bin/grep.exe | head -3
Binary file %s matches
binary files are TYPE

Zero is not absence. A nonzero count is not completeness either. A case-sensitive search over mixed-case data returns some arbitrary subset and reports success, which is strictly worse than returning nothing.

The diagnostic destroys the evidence

Here is the part that makes this cost hours rather than minutes. Having got a suspicious result, the obvious next move is to drop -c and look at what matched:

$ grep -i 'admin_token' inventario.bin
Binary file inventario.bin matches

That is the entire output. One NUL byte anywhere in the file is enough for grep to classify it as binary and suppress the matching lines. So the confirmation step returns *less* information than the count did: you can no longer see whether the match was admin_token, ADMIN_TOKEN, or something that merely contains them.

-a is what you wanted:

$ grep -ai 'admin_token' inventario.bin
role=admin_token

The house rule applies cleanly: the step you take to check the result is the step that removes the result.

-i is not the end of it

Having been burned once, the reflex is to add -i to everything. Measured, that is necessary and not sufficient. Two ways -i still returns zero on a string that is definitely in the file.

Case folding depends on the locale

The fixture contains city=münchen, lowercase, UTF-8. Searching for the uppercase form:

$ for L in C C.UTF-8 en_US.utf8 de_DE.utf8; do \
    printf "LC_ALL=%-12s " "$L"; LC_ALL=$L grep -ci 'MÜNCHEN' inventario.bin; done
LC_ALL=C            0
LC_ALL=C.UTF-8      1
LC_ALL=en_US.utf8   1
LC_ALL=de_DE.utf8   1

Under LC_ALL=C, -i returns 0. Not an error, not a warning about the locale — zero. And the control for that specific row, so it cannot be dismissed as the file being unreadable in that locale:

$ LC_ALL=C grep -c 'münchen' inventario.bin
1

Same locale, same file, exact lowercase form: found. So in LC_ALL=C the bytes are reachable and -i simply does not fold Ü to ü. ASCII case folding is all the C locale promises, and a one-byte-per-character locale cannot fold a two-byte character. Scripts that set LC_ALL=C for speed or for stable sort order inherit this.

A different encoding is invisible to -i entirely

The fixture also contains the word secretkey stored as UTF-16LE — one byte of text, one NUL, repeating. Every case variant:

$ for p in secretkey SECRETKEY SecretKey; do \
    printf "grep -aic %-10s -> " "$p"; grep -aic "$p" inventario.bin; done
grep -aic secretkey  -> 0
grep -aic SECRETKEY  -> 0
grep -aic SecretKey  -> 0

Three zeros, and the bytes are plainly visible one command over:

$ xxd inventario.bin | sed -n '4,5p'
00000030: c3bc 6e63 6865 6e0a 0073 0065 0063 0072  ..nchen..s.e.c.r
00000040: 0065 0074 006b 0065 0079 000a 656e 640a  .e.t.k.e.y..end.

s.e.c.r.e.t.k.e.y is right there in the ASCII column. -i cannot help, because the problem is not case: there is no byte sequence secretkey in the file at all. A byte search finds it at offset 57:

$ python -I -c "import io; d=io.open('inventario.bin','rb').read(); \
    print(d.find('secretkey'.encode('utf-16-le')))"
57

And the usual escape hatch for this, grep -P with \x00 between the letters, is not available in the grep shipped with Git for Windows — under every locale tried, including C:

$ LC_ALL=C grep -aoP 's\x00e\x00c\x00r\x00e\x00t' inventario.bin
grep: -P supports only unibyte and UTF-8 locales
$ echo abc | LC_ALL=C grep -P 'a.c'
grep: -P supports only unibyte and UTF-8 locales

The second command is the control: -P is failing for every pattern, not for this one. Whatever that build is objecting to, PCRE mode is unusable here, so on this machine the byte-level search has to happen outside grep.

Note also that decoding the whole file as UTF-16 is not a general fix. In this fixture the text starts at offset 57, an odd number, so whole-file UTF-16 decoding pairs the bytes one position out and finds nothing:

utf-8      contains secretkey: False
utf-16-le  contains secretkey: False

Both False, while the byte search above returns 57. Alignment is a third thing that can produce a zero.

What each zero actually means

you ranyou gotwhat it rules out
grep -c 'ADMIN_TOKEN' f0, exit 1that exact casing, nothing more
grep -ci 'ADMIN_TOKEN' f1—
LC_ALL=C grep -ci 'MÜNCHEN' f0that casing, plus ASCII folds of it
grep -aic 'secretkey' f0that byte sequence in single-byte form
grep -ai 'admin_token' frole=admin_tokenpresent, and now visible
grep -i 'admin_token' fBinary file f matchespresent, details suppressed

Every one of those zeros is a true statement about a narrow question. None of them is the statement "this string is not in this file".

What to run instead

grep -ai, always, on anything you did not write

grep -ai 'pattern' file

-i for the casing, -a so you can see what actually matched and judge it yourself. The cost of -i is false positives, which you can look at; the cost of omitting it is false negatives, which you cannot.

Make the file's own casing the measurement

Do not ask whether your pattern is present. Ask what is actually in there, and read it:

$ grep -aio 'binary file[a-z %]*' /usr/bin/grep.exe
Binary file %s matches
binary files are TYPE

Two lines, two different casings, no guessing about which one to search for.

Pin the locale deliberately, in the direction you want

LC_ALL=C is the fast, deterministic, ASCII-only choice and that is a trade-off, not a default. If the data can contain non-ASCII, LC_ALL=C.UTF-8 folds it; measured above as 1 where C gave 0.

Treat exit 1 as "not found in this encoding"

Before concluding absence from a zero, confirm the search could succeed on that file at all, with a string you know is present. That one extra command is the whole difference between a measurement and an assumption — and it is the same discipline as any other negative result on this site.

Why this one is worth a page

  1. Exit 1 is unambiguous and means something narrower than it reads. The

tool is not wrong; "no line matched this pattern" is exactly true.

  1. Nonzero is not safe either. Over mixed-case data a case-sensitive search

quietly returns a subset, with a successful exit status.

  1. The confirming step hides the answer. On binary input, looking at the

match replaces it with Binary file ... matches.

  1. The reflex fix has two holes of its own — the C locale does not fold

non-ASCII, and no amount of case folding finds a different encoding.

The general form: a search result describes the search. To say anything about the data, the search has to be shown capable of finding what is there.

Measured on

Windows 11, Git Bash MSYSTEM=MINGW64, GNU grep 3.0 from Git for Windows (/usr/bin/grep.exe, 213710 bytes, dated 12 Feb 2026), default LC_CTYPE=C.UTF-8, Python 3.14 standard library only, 2026-11-27. inventario.bin is an 80-byte fixture written specifically for this page and is identified as such throughout; the grep.exe measurements are on a real binary nobody built for this. Locale availability varies by installation — locale -a on this machine lists C, C.utf8, en_US.utf8 and de_DE.utf8 among others, and a machine without them will behave differently. No claim is made about strings or iconv: neither is installed here, so neither was tested.

← all notes