Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

According to the grep manual:

  * The preceding item will be matched zero or more times.
  + The preceding item will be matched one or more times.

Let's test it

echo 'agb' | grep 'a.*b' # returns agb
echo 'agb' | grep 'a.+b' # returns nothing

Why + did not match three gs? According to my knowledge 3 is more than 1.

share|improve this question
The + quantifier has to be written as \+ in BRE. So either grep 'a.\+b' or grep -E 'a.+b'. – manatwork Sep 23 '13 at 14:22
up vote 2 down vote accepted

Whichever grep manual you're reading should also have a section explaining the different kinds of regular expressions (basic and extended). the + operator is not available in basic, only extended. To use extended regular expressions, you need the -E option.

share|improve this answer

Much of what we think of as "regular expressions" are actually called Extended Regular Expressions (or EREs) in POSIX. But your grep call appears to be in Basic Regular Expression mode (or BRE, for short). BREs and EREs have a number of differences. One of these differences is that you have to escape metacharacters: + is a literal plus sign unless you escape it with a backslash. Your grep command appears to be in BRE mode, so try using \+ instead of a plain +.

If you're being really strict about BRE versus ERE, then BREs don't actually support the + and ? operations, though you can simulate them with \{1,\} and \{0,1\} constructs, respectively. Strict BREs don't support the | operator either, and I'm not sure how you simulate that in a BRE. However, unlike EREs, BREs support backreferences (which look a lot like they do in Perl, except that you have to escape the parens).

Some grep implementations (like GNU) support the ?, +, and | operators in BRE mode, though you have to escape them like any other metacharacters: \?, \+, and \|. But no grep implementations that I'm aware of support backreferencing in ERE mode.

To force your grep to use ERE mode, you can use the -E option to grep, or you can call it as egrep instead.

share|improve this answer
GNU grep -E supports backreferences which you can verify with echo aa | grep -E '(.)\1'. – Stéphane Chazelas Sep 23 '13 at 16:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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