简体   繁体   中英

How can I send error output to both stdout and file in bash

If I use this

cmd 2>/var/error.log

Then my error goes to that file but then I can't see on screen.

Is there any way I can simultaneously show it on screen as well as send to file?

This will display both stdout and stderr on the terminal while only sending stderr to err.log :

cmd 2> >(tee err.log >&2)

>(...) is process substitution . (The space between the two consecutive > is essential.) This sends stderr and only stderr to the tee command.

The >&2 causes the error messages remain in stderr. This would be important, for example, if this line occurs inside some script whose stdin or stderr is being redirected. (Hat tip: Chepner.)

cmd 2>&1 | tee /tmp/error.log

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM