Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
[Computer]$ grep "foo|bar" filename

I understand the above command should return each line in filename where there exits "foo" or "bar". The man pages confirms | as the Regex or symbol and the code works for "foo" and "bar" independently. What am I missing?

share|improve this question
up vote 14 down vote accepted

grep uses basic regular expressions (BRE) by default. From the man page:

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

So you either have to escape the |:

grep "foo\|bar" filename 

or turn on extended regular expressions:

grep -E "foo|bar" filename
share|improve this answer
I saw that in the man page and misunderstood. I thought it meant that \| would make it look for the complete expression "foo|bar" as opposed to the individual expressions. Thanks for the fast response! – Ocasta Eshu Jun 13 '12 at 1:23
You can also use egrep (an alias for grep -E) instead of grep. – speakr Aug 2 '12 at 12:52

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.