What are the differences between grep, fgrep and egrep? What are some examples of using them differently?
2 Answers
See man grep:
The variant programs
egrep,fgrepandrgrepare the same asgrep -E,grep -F, andgrep -r, respectively. These variants are deprecated, but are provided for backward compatibility.
And
Pattern Syntax -E, --extended-regexp Interpret PATTERNS as extended regular expressions (EREs, see below). -F, --fixed-strings Interpret PATTERNS as fixed strings, not regular expressions. -G, --basic-regexp Interpret PATTERNS as basic regular expressions (BREs, see below). This is the default. -P, --perl-regexp Interpret PATTERNS as Perl-compatible regular expressions (PCREs). This option is experimental when combined with the -z (--null-data) option, and grep -P may warn of unimplemented features.- Prefer
grep -Fwhenever you have no pattern to match as it is a lot faster. - Use
grep -Eand especiallygrep -Ponly when Basic Regex does not fullfil your needs as the matching will be slower.
What's Difference Between Grep, Egrep and Fgrep in Linux? - TecMint explains the difference well. All three do the same thing with the same code, but have different options.
egrep is the same as grep-E, interpreting PATTERN as an extended regular expression. It treats meta-characters as is and doesn't substitute them as strings like in the original grep. GNU's grep, which is used by Ubuntu, has no difference in available functionality between basic and extended syntax (unlike the original grep found in some other distros).
fgrep is the same as grep-F. interpreting PATTERN as a list of fixed strings (instead of regular expressions), separated by newlines, any of which is to be matched. It doesn't recognize regular expressions, nor any meta-characters. For searching any direct string, it's faster, so this is the version of grep which should be selected.