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

When using fgrep, the man page says it will

Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.

In bash, what's the correct way to insert newlines into the PATTERN argument (i.e. such that it'll match any of the lines according to the man page).

I've tried the following with no luck:

fgrep word1\nword2
fgrep word1\rword2
fgrep "word1\nword2"
fgrep "word1\rword2"

I'd like the command to be all on one line if possible.

share|improve this question

This can be done by using the $'\n' syntax (see http://stackoverflow.com/a/3182519/3822464). So for example:

fgrep word1$'\n'word2

Or you can wrap the whole PATTERN that way (credit don_crissti)

fgrep $'word1\nword2\nword3'
share|improve this answer
grep -F 'word1
word2' infile

or, if you prefer on one line:

grep -F -e 'word1' -e 'word2' infile
share|improve this answer
Thanks - the -e is a good alternate solution – sa289 Jan 13 at 23:17

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.