Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does redirection + pipe ( 2>&1 |) merge both streams instead of moving stderr to stdout?

I read that redirections are processed left to right. So in this example

 command 2>&1 | less

One would think that fd 2 is directed to fd 1 first and then fd 1 is sent to pipe. So fd 1 and 2 point to separate places.

But actually here fd 1 and 2 both point to the pipe, because for some reason fd 1 is sent to pipe first and then fd 2 is sent to fd 1. Why are redirections processed right to left in this case?

like image 841
Ankur Agarwal Avatar asked Jan 19 '23 09:01

Ankur Agarwal


2 Answers

The pipe is not a redirection, so in fact redirections (of which there is only one in your example) are being processed the way you think. The pipe is a separate thing at the end.

like image 81
John Zwinck Avatar answered Mar 09 '23 06:03

John Zwinck


The reason is that pipes aren't the same as redirections. A redirect affects the one command, while a pipe joins two commands.

like image 20
Anomie Avatar answered Mar 09 '23 07:03

Anomie