Why grep

Why grep "error|failed" finds nothing in a log full of failures

●2 ●38 ●102
calendar_today • schedule1 min read

If grep returns empty on a file you know contains matches, suspect the pattern before the file. Default grep uses Basic Regular Expressions (BRE), where |, +, ?, and () are literal characters unless backslash-escaped. So grep "error|failed" app.log searches for the literal twelve-character string error|failed — pipe included — which no logger ever writes. Alternation needs Extended Regular Expressions via -E:

grep "error|failed" app.log     # BRE: literal string, matches nothing
grep -E "error|failed" app.log  # ERE: matches either word

The dangerous part is the exit behavior. An impossible pattern is not a syntax error, so grep exits 1 — the same code as "no matches in a clean file" — and prints nothing. A wrong question and a healthy log look identical, which is how this bug hides inside monitoring checks and cron pipelines for days.

Two related traps: -P (Perl regex, \d, lookaheads) is GNU-only and fails on macOS/BSD grep, and bare grep aimed at a directory refuses with "Is a directory" unless -r is set.

When I'm unsure what a pattern will match, I assemble the command in this grep command builder with a live match tester — paste a sample line you know should match, and a zero count tells you the pattern is the bug before the empty result gets believed.


I bundled the scripts I copy onto every new server, plus 10 bonus ones, into the Production Bash Toolkit. 30-day refund if it isn't useful.

Want more #linux & #bash deep dives?

Join 6,093+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

More Posts

A deploy guard that passes with no token

BashSnippets - Nov 24

How to Debug Failed n8n Workflows: Step-by-Step Guide

floxolab - Nov 2

Dozens of email accounts: learning AI coding tools as a student

rahuldk1105 - Oct 27

MCP Is the USB-C of AI. So Why Are You Plugging Everything In?

Ken W. Algerverified - Jun 10

Retry Failed n8n API Requests Without Duplicates

floxolab - Oct 14
chevron_left
3.8k Points • 142 Badges
North America • bashsnippets.xyz
79Posts
38Comments
14Connections
Linux user who got tired of Googling the same bash commands every time I sat down at a terminal. Sta... Show more

Related Jobs

View all jobs →

Commenters (This Week)

1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!