While reading through this page and a few others yields an answer, I thought I'd offer my own take. There's a TL;DR at the bottom.
First, egrep
is a shortcut for grep -E
and fgrep
is a short for grep -F
. Thus, there are three variants, and I'll discuss them.
fgrep string [list of files]
grep -F string [list of files]
In this variant, string is not a regular expression. It's just a string. There are no special characters. If you want to search for the filename foo.c, you can use grep -F foo.c
just like this, and it will work as expected. This is thus the simplest form of grep.
grep string [list of files]
In this form, your string is a basic regular expression. Only certain characters are used with their regular expression meaning, and others must be escaped with a backslant.
Let's make a simple example file:
File name: foo.c
File name: foo.C
foocc
Then I'll run grep twice:
$ grep 'foo.c' foo
File name: foo.c
foocc
$ grep 'foo\.c' foo
File name: foo.c
In the first example, the dot character is considered a meta character, and thus it matched both the dot in foo.c
as well as the first c
in foocc
.
In the second example, I escaped the dot character, erasing its regular expression usage, and thus it only matched a literal dot.
I'll come back to that. Now let's look at egrep.
egrep string [list of files]
grep -E string [list of files]
In this example, string is now an Extended Regular Expression. It otherwise works the same.
So what's the difference? Well, this is in the man page:
In basic regular expressions the meta-characters ?, +, {, |, (, and )
lose their special meaning; instead use the backslashed versions ?,
+, {, |, (, and ).
What does this all mean? Using either grep
or grep -E
or egrep
, you can do the exact same searching. The difference is what happens when you escape characters.
In BRE (basic regular expressions), the characters in that list lose special meaning unless you escape them. In ERE (extended regular expressions), you have to escape them if you don't want their special meanings.
So, these three commands are all equivalent:
grep '\(hello\|goodbye) cruel world+'
egrep '(hello|goodbye) cruel world\+'
grep -E '(hello|goodbye) cruel world\+'
In the first example, the parens and pipe characters are escaped, so they become meta characters. That is, we'll match either hello or goodbye. And the plus is not escaped, so it is taken literally (it's in the special list from above).
The last two examples are identical. The parents and pipe characters are not escaped, so they remain meta characters. The plus sign is escaped, so it becomes a literal.
So we'll only match lines that have a literal plus character after world. We'll match these:
hello cruel world+
goodbye cruel world+
We will not match anything else.
So to recap... The difference between grep
and grep -E
is which list of characters you escape for what, and that's it. BREs have a very short list of characters that are meta by default. EREs have a longer list. Regardless of which format you use, you can swap the meaning of a special character by escaping it.
One point of opinion: I prefer EREs. I don't have to think about which characters are which. I just use regular expressions and escape special characters if I need them as literals.