After 30mins of futile attempt to capture the output of wget
, I figured out that the program writes to stderr
rather than the stdout
. Searching in web and stack-overflow reveals this to be a well-known fact.
Any idea why is this so?
The wget command outputs the document content in a separate file by default. However, we can use the –output-document (-O) option to redirect the content to a file of our choice. As a particular use case, if we use – as the file, it directs the content to stdout.
So stdout by design is for data in a pipe, stderr is for errors or other messages to the user.
Only error logs should go to stderr. This is a pretty common convention supported by the 12factor design principles and the original intent behind io streams in operating systems. With all logs dumped to stderr, it's significantly more difficult to sift through output from using pipes.
The regular output is sent to Standard Out (STDOUT) and the error messages are sent to Standard Error (STDERR). When you redirect console output using the > symbol, you are only redirecting STDOUT. In order to redirect STDERR, you have to specify 2> for the redirection symbol.
It's well known, because it's in the manual.
Reporting messages on stderr
is common, because messages are separated from regular output on stdout
. This is useful when you combine several tools with a pipe. In this case it would be bad, when regular output and diagnostic messages were mixed up.
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