When do you need grep -E, and what is the difference from basic grep patterns?
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
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.