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