Grep Dialects (egrep, fgrep, PCRE)
If you read old shell scripts or Stack Overflow posts, you will frequently see commands like egrep and fgrep.
These are historical variants of grep that evolved into the modern flags we use today. Understanding their origins is crucial for maintaining legacy systems.
1. The Deprecated Commands
Historically, the UNIX ecosystem had three distinct binaries:
grep: Basic Regular Expressions (BRE).egrep: Extended Regular Expressions (ERE). Introduced by Alfred Aho to support full regex grammar.fgrep: Fixed-string Grep. Optimized using the Aho-Corasick algorithm to search for literal strings incredibly fast without any regex overhead.
The Modern Consolidation
In modern GNU Coreutils, egrep and fgrep are deprecated. If you run them on a modern Linux distribution, you will likely see a warning:
egrep: warning: egrep is obsolescent; using grep -E
They have been replaced by flags within the single grep binary:
| Old Command | Modern Equivalent | Description |
|---|---|---|
grep | grep | Basic Regular Expressions (BRE) |
egrep | grep -E | Extended Regular Expressions (ERE) |
fgrep | grep -F | Fixed/Literal Strings (No Regex) |
Best Practice: Always use the flags (-E, -F) in your scripts. Never use egrep or fgrep.
2. PCRE (grep -P)
While -E provides modern POSIX regex, some developers need the absolute power of Perl Compatible Regular Expressions (PCRE), which include features like look-arounds.
GNU grep implements this via the -P flag.
Look-arounds Example
Imagine you want to extract the value of a JSON key, but not the key itself.
{"user_id": 12345, "status": "active"}
Using PCRE lookbehinds, you can assert that "user_id": " precedes the numbers, without actually including that text in the match.
# The -o flag tells grep to ONLY print the matched text, not the whole line.
# (?<=...) is a lookbehind assertion.
echo '{"user_id": 12345}' | grep -P -o '(?<="user_id": )\d+'
# Output: 12345
The -P flag is highly specific to GNU grep. It does not exist on BSD (macOS) grep, nor does it exist in the lightweight grep shipped with Alpine/Busybox containers.
If you rely on -P in a Docker ENTRYPOINT script, it will crash on Alpine.