Curious Minds

Clear answers to science and history questions

How egrep Works: Extended Regex Explained

· 6 min read · Science

egrep is a command-line text-search tool that scans input line by line and prints every line matching an extended regular expression. It is functionally equivalent to running grep -E: the name is a historical shorthand for "extended grep," and modern implementations treat the two as the same program.

What egrep Actually Is

egrep began life in the 1980s as a separate binary shipped alongside grep in Unix System V and later in GNU grep. The original grep understood only basic regular expressions (BRE), where metacharacters such as +, ?, |, ( and ) had to be backslash-escaped to act as operators. egrep was introduced to read extended regular expressions (ERE), in which those characters are operators by default and a literal plus or pipe is written with a backslash.

GNU grep, the version found on most Linux systems and shipped with macOS as BSD grep, merged the two code paths long ago. The egrep binary is now a thin wrapper that simply sets the extended-regex flag. Because of that, the POSIX standard dropped egrep as a required utility, and some distributions have deprecated the name. It still works, but grep -E is the portable spelling and the one to prefer in scripts.

How the Matching Engine Works

egrep reads its input as a stream of lines. For each line, the regex engine attempts to find a match anywhere in the line unless the pattern is anchored. If a match is found, the whole line is written to standard output; if not, the line is silently skipped. Matching is case-sensitive by default and operates on bytes unless a locale with multibyte characters is active.

The engine itself is a hybrid. GNU grep uses a fast literal-string scanner (the Aho–Corasick algorithm) when the pattern contains no metacharacters, a Boyer–Moore style search for simple patterns, and a backtracking or DFA-based matcher for complex expressions. That is why a plain fixed string is much faster than a heavily alternated regex over the same file. The practical consequence is that egrep is usually fast enough to scan gigabytes of logs, but pathological patterns with nested quantifiers can still be slow.

Two behaviours surprise newcomers. First, egrep matches per line, so a pattern cannot span a newline; multi-line matching requires tools such as pcre2grep -M or awk. Second, the default output is the matching line, not the matched text. To see only the matched portion, use grep -Eo.

Extended Regular Expression Syntax

The ERE dialect adds several operators that BRE lacks or requires escaping. The core set is small but expressive:

  • . matches any single character except newline.
  • * matches zero or more of the preceding element; + matches one or more; ? matches zero or one.
  • | is alternation, so cat|dog matches either word.
  • () groups subexpressions, e.g. (ab)+ matches ab, abab, and so on.
  • {n,m} repeats the preceding element between n and m times.
  • [abc] is a bracket expression matching one character from the set; [^abc] negates it.
  • ^ and $ anchor the match to the start and end of a line.
  • \< and \> (GNU extensions) match word boundaries; POSIX uses [[:<:]] and [[:>:]].

Character classes such as [[:digit:]], [[:alpha:]] and [[:space:]] are portable across locales, unlike [0-9], which can behave differently under some collation rules. Backreferences like \1 are not part of POSIX ERE; GNU grep supports them as an extension, but other egrep implementations may not.

Common Options and Worked Examples

egrep accepts the same option set as grep. The most useful flags change what is printed or how matching is done:

  • -i ignores case.
  • -v inverts the match, printing lines that do not match.
  • -c prints a count of matching lines instead of the lines.
  • -n prefixes each line with its line number.
  • -l prints only the names of files containing a match.
  • -r recurses through directories.
  • -w requires the match to be a whole word.
  • -o prints only the matched text, one match per line.

A few concrete invocations show how the pieces combine. To find failed logins in an auth log while ignoring case: egrep -i 'failed|invalid' /var/log/auth.log. To list every C source file that includes a header from a local directory: egrep -rl '#include "local/' src/. To count lines containing an IPv4 address: egrep -c '([0-9]{1,3}\.){3}[0-9]{1,3}' access.log. To extract just the timestamps from a log: egrep -o '^[0-9]{4}-[0-9]{2}-[0-9]{2}' app.log.

Quoting matters. Single quotes prevent the shell from expanding $, * and backslashes, so patterns should almost always be single-quoted. A pattern containing a literal single quote is awkward; use double quotes and escape the shell-sensitive characters, or switch to a character class such as [\x27] in GNU grep.

egrep Versus grep, fgrep and ripgrep

The grep family splits by regex dialect and search strategy. Understanding the differences prevents the wrong tool being chosen:

  • grep uses basic regular expressions; metacharacters like + and | are literal unless escaped.
  • egrep (or grep -E) uses extended regular expressions, where those characters are operators.
  • fgrep (or grep -F) treats the pattern as a fixed string with no metacharacters, making it the fastest choice for literal searches.
  • ripgrep (rg) is a separate modern tool that uses Rust regex syntax, respects .gitignore by default, and is generally faster on large trees, but is not part of POSIX.

A common misconception is that egrep is a different program with different features. In practice, on GNU and BSD systems it is the same binary invoked under a different name, and the only meaningful difference is the regex dialect. Another misconception is that egrep is deprecated and will stop working; it is deprecated in the sense that POSIX no longer requires it, but it remains present in GNU grep and BSD grep for compatibility.

Frequently Asked Questions

Is egrep the same as grep -E?

Yes. On GNU grep and BSD grep, egrep is implemented as an alias that enables extended regular expression syntax, exactly as grep -E does. The output and matching behaviour are identical.

Why does my egrep pattern with a plus sign not work?

In ERE, + is a quantifier meaning "one or more of the preceding element." To match a literal plus, escape it as \+ or place it in a bracket expression such as [+]. A pattern like C++ therefore needs to be written C\+\+.

Can egrep search across multiple lines?

No. egrep processes input line by line and cannot match a pattern that spans a newline. Tools such as awk, perl, or pcre2grep -M are needed for multi-line matching.

Does egrep support backreferences like \1?

POSIX ERE does not include backreferences. GNU grep accepts them as an extension, but portability suffers. If backreferences are required, use basic grep with escaped groups or switch to perl-compatible tools.

How do I search binary files with egrep?

By default grep detects binary data and prints a short "Binary file matches" message instead of the matching lines. Use -a to treat binary files as text, or -I to skip them entirely.

Is egrep safe to use in scripts?

It works, but grep -E is the portable and future-proof spelling because egrep is no longer required by POSIX and may be absent from minimal systems. Scripts that must run everywhere should use grep -E.

In this article
  1. What egrep Actually Is
  2. How the Matching Engine Works
  3. Extended Regular Expression Syntax
  4. Common Options and Worked Examples
  5. egrep Versus grep, fgrep and ripgrep
  6. Frequently Asked Questions