Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does dos2unix print to stderr?

Tags:

linux

dos2unix

When running dos2unix on a file I get the following printed to the terminal

dos2unix: converting file <filename> to UNIX format ...

In my attempt to suppress the output by sending it to /dev/null I noticed that this is sent out on stderr instead of stdout as I'd expected (since it seems like a normal message, not an error). Is there a reason for this?

like image 804
user1881282 Avatar asked Jul 13 '14 07:07

user1881282


2 Answers

In Unix-like environments it is common to chain processes: The result of one program is used as input for another program. Mixing results with diagnostics would confuse the next processing stage. It would also hide the diagnostics from a potential user watching the terminal, where processing results piped to the next program don't show.

This is the reason for the separation of results and diagnostics in stdout and stderr. Diagnostics are not restricted to errors but should contain everything which is not a processing result which subsequent programs would expect.

With respect to the actual question: dos2unix is often used to transform files in-place but can also output to stdout (when called without a file name, it reads from stdin and outputs to stdout). stdout then can be redirected independently from stderr. Consider cat blados | dos2unix > blaunix. You would still see the diagnostics (which may contain error messages!), but the result of the processing will go to blaunix.

It's not so common to print diagnostics at all in case of success — probably a little nod to DOS users. It would be pretty bad if the processing result contained the informational message; for example, it would break a C file.

like image 94
Peter - Reinstate Monica Avatar answered Oct 21 '22 18:10

Peter - Reinstate Monica


There is no reason, but normally stderr is not just for error output. It is another stream that is often used for logging or informational messages. Since a log message is not output, it is not sent to stdout, which is for the results of the program.

The reason its printed on your terminal is a consequence of your shell, and not really controlled by the application.

like image 6
Burhan Khalid Avatar answered Oct 21 '22 18:10

Burhan Khalid