user3214546
user3214546

Reputation: 6831

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?

Upvotes: 2

Views: 490

Answers (2)

John1024
John1024

Reputation: 113834

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.)

Upvotes: 3

Will Hartung
Will Hartung

Reputation: 118611

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

Upvotes: 0

Related Questions