user1881282 user1881282 - 5 months ago 37
Linux Question

Why does dos2unix print to stderr?

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?

Answer

dos2unix can output to stdout which 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.

If both the data and the diagnostics were on the same output stream they could not be separated. To accept input from stdin and write output to stdout is a common pattern in unix commands, and separating diagnostics to stderr therefore essential.

It's less common though to print diagnostics at all in case of success -- probably a little adaption to DOS users.