I'm a programming newbie and I'm trying to understand how stdin
, stdout
, and stderr
work. As I understand it, stdout
and stderr
are two different places where we can direct output from programs. I guess I don't understand what's the point of having a second "stream" of output just for errors with stderr
? Why not have errors on the regular stdout
? What does having errors on stderr
allow me to do (basically why is stderr
useful)?
There are two "points" to supporting distinct stout and stderr streams:
When you are writing applications that can be chained together (e.g. using pipelines) you don't want the "normal" output to get mixed up with errors, warnings, debug info and other "chit chat". Mixing them in the same stream would make life difficult for the next program in the chain / pipeline.
Example:
$ cat some-file | grep not
$ echo $?
If the cat
command did not write its error messages to stderr, then the grep
command would see a "file not found"
message if "some-file"
did not exist. It would then (incorrectly) match on the "not", and set the return code for the pipeline incorrectly. Constructing pipelines that coped with this sort of thing would be hellishly difficult.
Separate stdout and stderr streams have been support in (at least) UNIX and UNIX-like system since ... umm ... the 1970's. And they are part of the POSIX standard. If a new programming language's runtime libraries did not support this, then it would be considered to be crippled; i.e. unsuitable for writing production quality applications.
(In the history of programming languages, Python is still relatively new.)
However, nobody is forcing to write your applications to use stderr for its intended purpose. (Well ... maybe your future co-workers will :-) )