Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

echo to stderr and tee to log file?

Tags:

bash

In bash script,

echo "error" 1>&2 | tee -a log

will print stderr in screen but no log to file, how to do these at same time?

like image 870
timy Avatar asked Nov 13 '12 10:11

timy


People also ask

How do I redirect stderr and stdout to a log file?

Understanding the concept of redirections and file descriptors is very important when working on the command line. To redirect stderr and stdout , use the 2>&1 or &> constructs.

Does tee redirect stderr?

Redirect stderr Using the tee Command Standard errors are forwarded to the Command Line in Bash. Redirecting stderr might let you capture error messages in a distinct log file or eliminate the error messages completely.

How do I redirect stdout and stderr to a file in Linux?

Redirecting stdout and stderr to a file: The I/O streams can be redirected by putting the n> operator in use, where n is the file descriptor number. For redirecting stdout, we use “1>” and for stderr, “2>” is added as an operator. We have created a file named “sample.


1 Answers

To echo the text to both the log file and stderr, but not stdout, try this:

echo "error" | tee -a log 1>&2
like image 190
ams Avatar answered Oct 11 '22 18:10

ams