Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pipe here document command to log file

Tags:

bash

heredoc

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?

like image 838
Trent Avatar asked May 15 '13 01:05

Trent


People also ask

How do you pipe the output of a command to a file in Linux?

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.

How do I use HereDoc bash?

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.

What does <() mean in bash?

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.


2 Answers

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.

like image 83
Mark Reed Avatar answered Oct 15 '22 01:10

Mark Reed


An example with expr:

xargs expr << HEREDOC | tee output.log
10 + 11
HEREDOC
like image 35
perreal Avatar answered Oct 15 '22 02:10

perreal