logoalt Hacker News

time4teayesterday at 11:44 PM2 repliesview on HN

Useless use of cat error/award

But also | isnt a redirection, it takes stdout and pipes it to another program.

So, if you want stderr to go to stdout, so you can pipe it, you need to do it in order.

bob 2>&1 | prog

You usually dont want to do this though.


Replies

kazinatoryesterday at 11:51 PM

The point is that the order in which that is processed is not left to right.

First the | pipe is established as fd [1]. And then 2>&1 duplicates that pipe into [2]. I.e. right to left: opposite to left-to-right processing of redirections.

When you need to capture both standard error and standard output to a file, you must have them in this order:

  bob > file 2>&1
It cannot be:

  bob 2>&1 > file
Because then the 2>&1 redirection is performed first (and usually does nothing because stderr and stdout are already the same, pointing to your terminal). Then > file redirects only stdout.

But if you change > file to | process, then it's fine! process gets the combined error and regular output.

murphyslawtoday at 4:53 AM

You can pipe the fd directly:

# echo 1 >&2 2>| echo