16

I think stdout, so you can easily grep, what do you think?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

5 Answers5

13

Only errors go to stderr. This is in no way an error, it does exactly what the user had in mind, which is print usage information.

Deniz Dogan
  • 25,711
  • 35
  • 110
  • 162
  • Not "only errors"; **everything that is not output** goes to stderr. Logs go to stderr. Prompts go to stderr. Informational and diagnostic messages go to stderr. _But_ you are right: When a program is run with `--help`, the usage message is the intended output, so it belongs on stdout. – Charles Duffy Aug 29 '23 at 22:18
10

stdout if the user requested it with --help for example, makes it easier to pipe to less, grep it etc.

stderr if you are showing the help text because there was a problem, e.g. parsing the command line arguments.

Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
  • Well, (s)he specifically said "app --help" so I assume that "--help" is recognized as a command line argument. – Deniz Dogan Jul 01 '09 at 09:05
  • I was just suggesting a case where you might send help to stderr – Paul Dixon Jul 01 '09 at 10:58
  • Not "might" use stderr but "should" use stderr in the non-`--help` case: If you're showing the help text because there was a usage problem, you don't want that usage message to be mixed up with your output (which might be going to a file or into a pipeline or to somewhere else the operator won't get a chance to see it). – Charles Duffy Aug 29 '23 at 22:20
  • I agree. It's been 14 years, so I've improved it! – Paul Dixon Aug 31 '23 at 11:21
9

Well, it's an explicit request for help so it's output. If for some reason you can't output the help or the user mis-spells "help" then, by all means, send that to error :-)

Users that know what they're doing can use the infamous "2>&1" if they want errors on standard output.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1

It's not an error, so I'd say stdout....

Greg
  • 316,276
  • 54
  • 369
  • 333
  • **stderr is not only for errors** -- it's for absolutely everything intended to be read by a human operator rather than sent through whatever pipeline or file is receiving the program's intended output. (But then, when called with `--help`, a usage message _is_ the intended output; so this is right that stdout is the correct place, but faulty in the reasoning used to get there). – Charles Duffy Aug 29 '23 at 22:22
0

netcat is the only application I can think of that would redirect -h to stderr, and I can't for the life of me fathom why.

I suppose if you're outputting the help information because someone used improper arguments, you might want to redirect it to stderr, but personally even then I wouldn't use stderr because I don't think spamming error logs with fullblown help text is useful - I'd rather just output a single error pointing out the arguments were malformed to stderr. If someone is explicitly calling your application using -h or --help, then you really shouldn't redirect it to stderr.

Coding With Style
  • 1,692
  • 1
  • 14
  • 10
  • stderr isn't just for error logs -- it's for _display to the human operator_, which is why (for example) shell prompts go to stderr rather than stdout. stdout goes into pipelines, so it'll often be parsed or consumed by a separate program. (The word of POSIX is that stderr is for "diagnostic" content, whereas stdout is only for output; knowing when a program is ready for more input, as indicated by a prompt, falls into the diagnostic category). – Charles Duffy Aug 29 '23 at 22:23