Grep Options – Difference Between -e and -E

grep

I am trying to understand the difference between grep -e and grep -E. Now from grep manpage I got:

-E, –extended-regexp

Interpret PATTERN as an extended regular expression (see below).

-e PATTERN, –regexp=PATTERN

Use PATTERN as the pattern; useful to protect patterns beginning with –

The above explanation does not make sense for me.

So, can someone explain it to me using examples what is the difference between the two and when to use which option.

PS: Version: grep (GNU grep) 2.10

Best Answer

-e is strictly the flag for indicating the pattern you want to match against. -E controls whether you need to escape certain special characters.

man grep explains -E it a bit more:

   Basic vs Extended Regular Expressions
   In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, \+, \{, \|, \(, and \).

   Traditional  egrep  did  not  support  the  {  meta-character, and some egrep implementations support \{ instead, so portable scripts should avoid { in grep -E patterns and should use [{] to match a
   literal {.

   GNU grep -E attempts to support traditional usage by assuming that { is not special if it would be the start of an invalid interval specification.  For example, the command grep -E '{1' searches for
   the two-character string {1 instead of reporting a syntax error in the regular expression.  POSIX.2 allows this behavior as an extension, but portable scripts should avoid it.
Related Question