Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Redirection bug for stderr?



On Thu, 20 Nov 2014 11:44:40 +0100
Pierre Neidhardt <ambrevar@xxxxxxxxx> wrote:
> I recently noticed a somewhat unexpected behaviour with a configuration-less zsh 5.0.7:
> 
> 	 $ ls /root >/dev/null 2>&1
>    $ ls /root 2>/dev/null | cat
>    $ ls /root >/dev/null 2>&1 | cat
> 	 ls: cannot open directory /root: Permission denied
> 
> I believe last error line should not appear.

I suspect you're hitting this.

3.26: Why is my output duplicated with `foo 2>&1 >foo.out | bar'?

  This is a slightly unexpected effect of the option MULTIOS, which is
  set by default.  Let's look more closely:

    foo 2>&1 >foo.out | bar

  What you're probably expecting is that the command `foo' sends its
  standard output to the pipe and so to the input of the command `bar',
  while it sends its standard error to the file `foo.out'.  What you
  actually see is that the output is going both to the pipe and into the
  file.  To be more explicit, here's the same example with real commands:

    % { print output; print error >&2 } 2>&1 >foo.out | sed 's/error/erratic'
    erratic
    output
    % cat foo.out
    output

  and you can see `output' appears twice.

  It becomes clearer what's going on if we write:

    % print output >foo1.out >foo2.out
    % cat foo1.out
    output
    % cat foo2.out
    output

  You might recognise this as a standard feature of zsh, called `multios'
  and controlled by the option of the same name, whereby output is copied
  to both files when the redirector appears twice.  What's going on in the
  first example is exactly the same, however the second redirector is
  disguised as a pipe.  So if you want to turn this effect off, you need
  to unset the option `MULTIOS'.



Messages sorted by: Reverse Date, Date, Thread, Author