Skip to content
Latchkey

grep -E (egrep): Extended Regular Expressions

grep -E interprets the pattern as an extended regular expression, where +, ?, |, {}, and () are special without escaping.

Basic grep requires backslashes for alternation and grouping. -E (the old egrep) makes those metacharacters work plainly, which is what most people expect from a regex.

What it does

grep -E (--extended-regexp) switches to POSIX extended regular expressions (ERE). In ERE, +, ?, {n,m}, | (alternation), and () (grouping) are metacharacters directly, with no backslash. The standalone command egrep is deprecated in favor of grep -E.

Common usage

Terminal
grep -E 'error|warning|fatal' app.log
grep -E '^v[0-9]+\.[0-9]+' tags.txt
grep -E 'HTTP/[12](\.[0-9])?' access.log

Options

TokenMeaning in ERE
|Alternation: match either side
+One or more of the preceding item
?Zero or one of the preceding item
{n,m}Between n and m repetitions
( )Grouping

In CI

Use -E whenever you need alternation, since grep "a|b" without -E looks for the literal string "a|b". Recent GNU grep prints "egrep: warning: egrep is obsolescent" when you call egrep, so update scripts to grep -E to keep logs clean.

Common errors in CI

A pattern that "never matches" with alternation is usually missing -E, so the | is taken literally. Conversely, in -E mode a literal ( or + must be escaped or it is read as a metacharacter, producing too many or too few matches. BSD/macOS grep supports -E, but some bracket and backreference behaviors differ from GNU; test on the runner OS you target.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →