Subject: Re: Redirection order with multios option set
From: Peter Stephenson <[email-masked]>
To: Cuong Manh Le <[email-masked]>, Zsh hackers list <[email-masked]>
Date: Mon, 04 Apr 2016 11:53:44 +0100
List-Id: Zsh Workers List <zsh-workers.zsh.org>

On Mon, 04 Apr 2016 17:27:30 +0700
Cuong Manh Le <[email-masked]> wrote:
> $ echo a >/tmp/test >&1
>
> write two lines `a` to file `/tmp/test`.
>
> From user perspective, this behavior seems to be in-consistent to me.

It's expected --- redirections are evaluated in order.  There is no
memory of what stdout used to be before we started evaluating
redirections, which is what you'd need to get this to do what you
thought.

However, it could do with being documented.  See if this explains it.

pws

diff --git a/Doc/Zsh/redirect.yo b/Doc/Zsh/redirect.yo
index 35fa773..66160a6 100644
--- a/Doc/Zsh/redirect.yo
+++ b/Doc/Zsh/redirect.yo
@@ -234,6 +234,25 @@ example(date >foo | cat)
 
 writes the date to the file `tt(foo)', and also pipes it to cat.
 
+Note also that redirections are always expanded in order.  This happens
+regardless of the setting of the tt(MULTIOS) option, but with the option
+in effect there are additional consequences. For example,
+the meaning of the expression tt(>&1) will change after a previous
+redirection:
+
+example(date >&1 >output)
+
+In the case above, the tt(>&1) refers to the standard output at the
+start of the line; the result is similar to the tt(tee) command.
+However, consider:
+
+exmaple(date >output >&1)
+
+As redirections are evaluated in order, when the tt(>&1) is encountered
+the standard output is set to the file tt(output) and another copy of
+the output is therefore sent to that file.  This is unlikely to be what
+is intended.
+
 If the tt(MULTIOS)
 option is set, the word after a redirection operator is also subjected
 to filename generation (globbing).  Thus