60

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

2
  • See also: unix.stackexchange.com/questions/17949/… Oct 10, 2012 at 19:20
  • 4
    The purpose of -e is really just to disambiguate when a regex starts with a dash. So grep ---foo gives unrecognized option: ---foo but you can say grep -e ---foo to grep for the regular expression ---foo.
    – tripleee
    Oct 10, 2012 at 19:31

4 Answers 4

43

-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.
4
  • 6
    so this is the reason when I do echo "hello" | grep -o -e 'h|l' I get no output because | lost its special meaning and when I do echo "hello" | grep -o -E 'h|l' I get the desired output.
    – ronnie
    Oct 10, 2012 at 19:07
  • 3
    Yes. echo "hello" | grep -o -e 'h' -e 'l' will work if you want to toss the -E away in this case.
    – hafichuk
    Oct 10, 2012 at 19:11
  • 5
    grep -e -o 'h|l' will match literally h|l while grep -e -o 'h\|l' will match h or l and with -E it's the contrary Oct 10, 2012 at 19:24
  • @ronnie it's unfortunate you need 200+ reputation to have that "feature" follow you between SE sites...
    – hafichuk
    Oct 10, 2012 at 19:39
10

Also grep -e allows to use several strings for searching: 'grep -e 'abc' -e 'def' -e '123' will look for any of the three of these strings: abc as well as def and 123.

This works quite similar to grep 'abc\|def\|123' where \| stands for or but could be a bit clearer to read.

As the most important facts on grep -E are already explained here, I just want to add what I summed up on this topic on a quite similar question: Regular Expression for finding double characters in Bash

4

Just to elaborate on the -e option. -e is often optional:

grep PATTERN

is identical to

grep -e PATTERN

unless, as stated in an earlier Answer and in the man pages, there are multiple search patterns, or to protect a pattern beginning with a hyphen (-).

3

see below

/extended

grep understands three different versions of regular expression syntax: “basic,” “extended” and “perl.” In GNU grep, there is no difference in available functionality between basic and extended syntaxes. In other implementations, basic regular expressions are less powerful. The following description applies to extended regular expressions; differences for basic regular expressions are summarized afterwards. Perl regular expressions give additional functionality, and are documented in pcresyntax(3) and pcrepattern(3), but may not be available on every system.

So, once again.

In GNU grep, there is no difference in available functionality between basic and extended syntaxes

1
  • 3
    the functionality is the same between basic and extended but the syntax is slightly different. regexp special chars like (, ), | etc need to be backslash-escaped to have their special meaning in basic regexp, but not in extended (where they need to be escaped to be treated as a string literal)
    – cas
    Oct 11, 2012 at 5:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.