66

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

ronnie
  • 901
  • See also: http://unix.stackexchange.com/questions/17949/what-is-the-difference-between-grep-egrep-and-fgrep – Mechanical snail Oct 10 '12 at 19:20
  • 5
    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

4 Answers4

49

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

AkselA
  • 133
hafichuk
  • 606
  • 9
    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. –  Oct 10 '12 at 19:07
  • 4
    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
  • 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 – 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
12

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

asgs
  • 180
erch
  • 5,030
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 (-).

flow2k
  • 611
  • 1
  • 10
  • 19
  • Just to clarify, I believe -e is the same as -G, only that it can be used to pass multiple arguments. From the manual: "-G, --basic-regexp.. Interpret PATTERN as a basic regular expression (BRE, see below). This is the default." So there are still only 3 types of pattern matching, basic (default), extended and pcre). – alchemy Oct 12 '22 at 21:00
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

  • 5
    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 05:27