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.


This can be done by using the $'\n' syntax (see https://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'
grep -F 'word1
word2' infile

or, if you prefer on one line:

grep -F -e 'word1' -e 'word2' infile
  • Thanks - the -e is a good alternate solution – sa289 Jan 13 '16 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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