timy
timy

Reputation: 511

echo to stderr and tee to log file?

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?

Upvotes: 14

Views: 15115

Answers (4)

ams
ams

Reputation: 25559

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

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

Upvotes: 14

dogbane
dogbane

Reputation: 274522

To view both stdout and stderr on the console and send both streams to a log, redirect stderr to stdout as shown below:

progam.sh 2>&1 | tee -a log

Upvotes: 7

P.P
P.P

Reputation: 121347

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

With the first part 1>&2, what you are saying is: "Redirect stdout to stderr". So the echoed output "error" goes to stderr.

Pipe (|) only reads from stdout, not stderr. So tee doesn't get any stdin at all from the pipe. Hence, it appends nothing to the log file.

Upvotes: 8

Benjamin Bannier
Benjamin Bannier

Reputation: 58547

But default only stdout is passed along in pipes, so that in

$ echo "error" | tee 

tee only sees the stout from the echo, not the stderr. stderr will still be displayed in the terminal.

Upvotes: 0

Related Questions