[ACCEPTED]-Should I output warnings to STDERR or STDOUT?-stderr

Accepted answer
Score: 40

If I saved the output of this script (i.e. stdout 7 only) so that I could process it later, would 6 that warning interfere with how the output 5 is parsed? Moreover, if output is piped 4 to another process, the warning should show 3 up on the terminal, so the user sees it 2 immediately.

For those reasons, in general, you 1 output warnings to stderr.

Score: 11

The warning should go to stderr.

In addition 17 to the points presented by others (causing 16 parsing errors for downstream processes 15 and hiding the error from the user at the 14 console), there is an issue of flexibility.

If 13 the user does not want the warning from 12 stderr to go to a downstream process that 11 is parsing stdout, they don't have to do 10 anything special.

your_script | downstream_process

If the user wants the warning 9 from stderr to go to a downstream process 8 that will parse stdout and stderr, the user 7 can use 2>&1 to redirect stderr into 6 stdout.

your_script 2>&1 | downstream_process

If you output both warnings and any 5 normal data to stdout, the user has no good 4 way of separating the warnings from the 3 data without parsing everything. So sending 2 the warnings to stderr gives your script 1 more flexibility as well.

Score: 2

The real question is: if someone were to 8 redirect the output of your script to a 7 file, would you want the warning placed 6 in the file, or directed to the user?

If 5 you're expecting the user to take some action 4 as a result of the warning, it should go 3 to STDERR. If some downstream script is 2 likely to be tripped up by the warning, it 1 should go to STDERR.

More Related questions