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?
In Linux, for redirecting output to a file, utilize the ”>” and ”>>” redirection operators or the top command. Redirection allows you to save or redirect the output of a command in another file on your system. You can use it to save the outputs and use them later for different purposes.
To use here-document in any bash script, you have to use the symbol << followed by any delimiting identifier after any bash command and close the HereDoc by using the same delimiting identifier at the end of the text.
This is called process substitution. The <(list) syntax is supported by both, bash and zsh . It provides a way to pass the output of a command ( list ) to another command when using a pipe ( | ) is not possible.
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.
An example with expr
:
xargs expr << HEREDOC | tee output.log
10 + 11
HEREDOC
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With