Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Pipe stderr, and not stdout" not working in Zsh

Tags:

shell

zsh

How to pipe stderr, and not stdout? perfecly captures my problem, and the first answer is exactly how I initially tried to solve it:

(echo stdout; echo 1>&2 stderr) 2>&1 >/dev/null | less

(The subshell command with echos is a minimal placeholder thanks to user1934428 to demonstrate the issue; my actual problem has a more useful command, but one that everyone else can't run.)

However, it doesn't work: It's showing both stdout and stderr together. If I remove the pipe, it works as expected, only showing stderr. Eventually I realized this might be a shell thing, and tried bash: It worked perfectly there. I'm using Zsh: what about Zsh makes this fail?

like image 691
D0SBoots Avatar asked Oct 15 '25 14:10

D0SBoots


1 Answers

This is due to MULTIOS, which duplicates the output streams when you supply multiple redirections. Pipes are implicit redirections.

Type ls >/dev/null | less in your zsh and you will see output even though you redirected it. That's multios duplicating the stream.

unsetopt multios will provide the desired behaviour. This is quite a handy feature if I wouldn't always forget about it until it irritates me.

http://zsh.sourceforge.net/Doc/Release/Redirection.html#Multios

like image 177
WolleTD Avatar answered Oct 18 '25 17:10

WolleTD