Hacker News new | ask | show | jobs
by heads 841 days ago
And at the command line: headers to stderr, body to stdout.

Without it you get this behavior:

  > heights | sort -k2 -nr
  George  186
  Wally   173
  NAME    HEIGHT
But if the headers bypass stdout then you get this instead:

  > heights | sort -k2 -nr
  NAME    HEIGHT
  George  186
  Wally   173
3 comments

> heights | (sed -u 1q; sort -k2 -nr)

does the right thing.

(from https://stackoverflow.com/a/56151840)

...unless you're writing to a file, in which case you'll require a couple tries and a higher quantum of shell comprehension.

This is a persistent limitation of the in-band text-only model. But I think this misuse of STDERR would be more confusing than helpful.

If `heights` was a file, you could do this:

  % head -1 heights ; sed 1,1d heights | sort -k2 -nr
If `heights` is an executable that would be expensive to call twice:

  % heights > /tmp/heights ; head -1 /tmp/heights ; sed 1,1d /tmp/heights | sort -k2 -nr
Or you could pipe to `awk`, etc.

Granted, these other options are less convenient, but they are also less surprising, and they work even if the executable creator has different ideas.

Sometimes you gotta do what you gotta do.

Which one works best here ? Headers to stderr, or headers only if stdout is a tty ?