Preparation and testing function:

echo -e 'q\nd' > patt

cat patt

echo 'qweasd' | grep --file=patt

If you specify 2 templates in the file, everything works according to the manual. How to get the same result by dividing one template into parts using a new line symbol?

Or is it an error in the manual?

-e patterns
Use patterns as one or more patterns; newlines within patterns separate each pattern from the next. If this option is used multiple times or is combined with the -f (--file) option, search for all patterns given. Typically patterns should be quoted when grep is used in a shell command. (-e is specified by POSIX.)

Only the specified method of separation is of interest, not these variants:

grep -e 'pattern1\|pattern2'
grep -e 'pattern1' -e 'pattern2'

1 Answer 1


The manual is accurate, but the method isn’t obvious; use a newline in the command line:

$ grep -e 'q
> d' <<<"qweasd"

That is to say, type grep -e 'q, then Enter, which should cause your shell to display the secondary prompt (> above), and then enter d' etc.

  • This method works, but is a similar result possible without additional user input, a one-line solution? I am trying to understand what was meant in the manual, I doubt that the developers meant this method. Is it possible to insert a new line character in any way without additional user input? Say, use the ASCII code of a character (with LC_ALL=C environment variable) between the lines of the pattern? But I don't know yet how to insert a character by its code.
    – nnlkcncff
    Nov 28, 2020 at 12:18
  • In some shells (including bash), you can use a $'...' string. So grep -e $'q\nd' <<<"qweasd Nov 28, 2020 at 13:18
  • Thank you so much! It is very similar to what I expected.
    – nnlkcncff
    Nov 28, 2020 at 14:58
  • @nnlkcncff you can use the technique described by roaima; if you’re in a shell script, splitting across two lines works without additional input. The split line can also be copied and pasted as-is, again without additional input. Nov 28, 2020 at 16:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .