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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With