Bash – How to insert newlines into PATTERN when using fgrep / grep -F / grep –fixed-strings

bashnewlinesquoting

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.

Best Answer

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'
Related Question