3

The following command produces different outputs in zsh v. 4.x and 5.x:

{
  (
    printf "X\nY\n"
    printf "WARNING: foo\nWARNING: bar\n" >&2
    printf "1\n2\n"
  ) 2>&1 >&3 | grep -v foo >&2
} 3>&1

(This code is supposed to illustrate how to selectively suppress some of the output sent to stderr by a program; in this case, the warning foo is being suppressed selectively, while the warning bar is allowed to go through; note that in the end, stderr and stdout streams remain separate.)

On v 4.x the output I see at the terminal is as desired/expected:

X
Y
1
2
WARNING: bar

(You may see a different ordering of the WARNING: bar line relative to the other ones.)

On v 5.0.7 (Debian) and 5.1.1 (Darwin), however, what I see at the terminal is this:

X
Y
1
2
WARNING: bar
X
Y
1
2

IOW, the lines that should be sent to stdout appear twice.

I figure there are three possibilities:

  1. this is a bug in v. 5.x (in which case my question is: is there a workaround?)
  2. I have not properly configured my v. 5.x zsh (in which case my question is: how should I configure my v. 5.x zsh to get the desired behavior?);
  3. there is a bug in v. 4.x, but not in v. 5.x, that masked a bug in my code (in which case my question is: how could I fix my code to get the desired behavior in v 5.x?)
1
  • 1
    It's worth mentioning that both dash and bash produce the same behaviour as zsh 4, which lends credence to the hypothesis that the original behaviour was correct.
    – Celada
    Mar 5 '16 at 19:42
2

Actually, the bug was in 4.0 and has been fixed.

What you're seeing is the effect of the multios options, specific to zsh. If you want to see the same behaviour as in other shells, you need to disable that option setopt nomultios or call zsh as sh.

With multios, in cmd1 >&3 | cmd2, you're redirecting cmd1's stdout to both &3 and the pipe to cmd2. See How can I pipe only stderr in zsh? for more details.

1
  • I was very curious about this question myself and I knew I could count on you coming along with the answer :-)
    – Celada
    Mar 6 '16 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.