Interview Q&A / Linux & Shell

IntermediateQuick answer1 min readUpdated 3 Oct 2026

When do you need grep -E, and what is the difference from basic grep patterns?

SHORT ANSWER

Basic grep treats +, ?, | and () as ordinary characters unless escaped. grep -E (extended regex) gives them their regex meaning, so grep -E 'cat|dog' matches either word. Use -F when you want a plain fixed string with no regex at all.

Explain it simply

A pattern is a mini-language. Basic grep understands only a small part of it; -E unlocks the rest, so you do not have to sprinkle backslashes.

With -E: | means or, + means one or more, ? means optional, {n,m} means repeat and () groups. So grep -E 'error|fail|fatal' app.log finds any of three words, and grep -E '^[0-9]{3}-' file finds lines that begin with three digits and a dash.

Without -E you must write grep 'error\|fail' and \(ab\)\+. Both styles work, but extended is easier to read. egrep is the old name for grep -E.

With -F (fixed strings), nothing is special: grep -F 'a.b*c' file looks for exactly that text. It is also faster and avoids surprises when searching for dots, brackets or dollar signs, such as a price like Rs.500.

-P enables Perl-style regex (lookaheads, \d) on GNU grep, but it is not portable.

Example

bash
printf 'ERROR disk\nWARN cpu\nINFO ok\nFATAL net\n' | grep -E 'ERROR|FATAL'
# ERROR disk
# FATAL net
echo 'price a.b' | grep -F 'a.b'

What to say in the interview

"grep -E enables extended regex, so |, +, ? and () work without backslashes. -F searches a literal string, which is safest for text with special characters. I use -E for alternatives like error|fatal."

Common mistakes

  • Using | in plain grep without escaping and matching a literal pipe.

#linux#text-processing#grep#regex

Related questions

Practise more Linux & Shell questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Linux & Shell quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.