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

migrated from stackoverflow.com Oct 10 '12 at 19:09

This question came from our site for professional and enthusiast programmers.

  • 3
    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 '12 at 19:31
up vote 24 down vote accepted

-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.
  • 2
    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 '12 at 19:07
  • 2
    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 '12 at 19:11
  • 3
    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 – Nahuel Fouilleul Oct 10 '12 at 19:24
  • @ronnie it's unfortunate you need 200+ reputation to have that "feature" follow you between SE sites... – hafichuk Oct 10 '12 at 19:39

Also grep -e allows to use several strings for searching: 'grep -e 'abc' -e 'def' -e '123' will look for all three of this 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

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

  • 2
    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 '12 at 5:27

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 (-).

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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