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 tim
To echo the text to both the log file and stderr, but not stdout, try this:
echo "error" | tee -a log 1>&2
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
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.
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.