14

Quite often I'll use the following construct to pipe output to a log file, keeping the output also on the display

./command 2>&1 | tee output.log

I'm trying to do something similar, but with using a here document:

./command << HEREDOC
params
HEREDOC 2>&1 | tee output.log

This doesn't work - is it possible to achieve this?

Trent
  • 2,328
  • 3
  • 33
  • 51

2 Answers2

22

Sure.

./command <<HEREDOC 2>&1 | tee output.log
params
HEREDOC

The here-document doesn't begin until the next line. The rest of the command is parsed as normal.

Mark Reed
  • 91,912
  • 16
  • 138
  • 175
  • 1
    works even with [line continuation](https://www.gnu.org/software/bash/manual/html_node/Escape-Character.html#Escape-Character) (``\``). For example, write ``./command <&1 | tee output.log`` in the second line. – myrdd Dec 04 '18 at 15:22
2

An example with expr:

xargs expr << HEREDOC | tee output.log
10 + 11
HEREDOC
perreal
  • 94,503
  • 21
  • 155
  • 181