Server Fault is a question and answer site for system and network administrators. 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

I'm console newbie. As I know, matching multiple patterns like this:


But, | character is pipe on console, so how can I specify those multiple patterns for grep?

share|improve this question
up vote 3 down vote accepted

You can escape the pipe and put the pattern in quotes:

grep "aaa\|bbb"

or use -E:

grep -E "aaa|bbb"


grep -E aaa\|bbb
share|improve this answer
The backslash in the first example is unnecessary; the shell removes it and grep never sees it, but the shell would pass the same info to grep if you omitted the backslash. – Jonathan Leffler Jun 27 '10 at 2:53
@Jonathan: Not so. Try this with and without the backslash. Only the one with the backslash will succeed. echo -e 'aaa\nbbb\nccc' | grep "aaa\|bbb" – Dennis Williamson Jun 27 '10 at 3:30
@Jonathan: You're better to read about escaping and quotation rule in ΤΖΩΤΖΙΟΥ's answer. – Eonil Jun 27 '10 at 8:42
Different shells ... and the question is meant to be about grep; oh well. Stuff that worked one way 20 years ago doesn't seem to work the same now - and I can't tell whether I'm misremembering or whether the shells have quietly changed behind my back. – Jonathan Leffler Jun 27 '10 at 20:02

Put "" between your pattern, like egrep "toto|name"

share|improve this answer

Or you go

grep -ie aaa -ie bbb filename

to grep for aaa or bbb in filename, case insensitively.

share|improve this answer
Useful information. Thanks! – Eonil Jun 27 '10 at 8:39

Lastly, you can put your patterns into a file, and use the -f flag. So grep -f patternlist.txt files. Where patternlist.txt is simply:

Make sure there are no empty lines, though.

--Christopher Karel

share|improve this answer

Enclose your pattern in single quotes:

grep -E 'aaa|bbb'

Should your pattern include an apostrophe, enclose it in double quotes:

grep -E "its|it's"

If it contains both single and double quotes, enclose it in double quotes and prefix with a backslash the characters ", $, ` and \:

grep -E "its|it's|letter \"e\"|pay \$20|\`a'|C:\\\\AUTOEXEC\\.BAT"

The final example of C:\AUTOEXEC.BAT takes into account that backslash is special both to the shell and to the regular expression syntax of egrep.

PS: The bash quoting page is a must-read.

share|improve this answer
Nice guidance for escaping and quotation. Thanks! – Eonil Jun 27 '10 at 8:41

egrep "aaa|bbb|ccc" <file>

will find all rows with either aaa OR bbb OR ccc in the file

egrep -i "aaa|bbb|ccc" <file>

will find aaa OR aaA OR aAa OR Aaa OR aAA OR AAa OR AAA OR ... and the same for every other string enumerated between the pipe | characters.

share|improve this answer

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.