zsh-workers
 help / color / mirror / code / Atom feed
* Redirection order with multios option set
@ 2016-04-04 10:27 Cuong Manh Le
  2016-04-04 10:53 ` Peter Stephenson
  0 siblings, 1 reply; 2+ messages in thread
From: Cuong Manh Le @ 2016-04-04 10:27 UTC (permalink / raw)
  To: Zsh hackers list

[-- Attachment #1: Type: text/plain, Size: 697 bytes --]

When multios option is set, the below two command are equivalent:

$ echo a >/tmp/test >/tmp/test1
$ echo a >/tmp/test1 >/tmp/test


Both command write `a` to `/tmp/test` and `/tmp/test1`.

Now to simulate `tee` command:

$ echo a >&1 >/tmp/test


or:

$ echo a >/dev/stdout >/tmp/test


work as expected, `a` was written to standard output as well as
`/tmp/test`. But swapping the redirection order produce different behavior:

$ 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.

I read the documentation but find no information about this case. Is this
behavior intended?

Cuong Manh Le
https://cuonglm.xyz

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Redirection order with multios option set
  2016-04-04 10:27 Redirection order with multios option set Cuong Manh Le
@ 2016-04-04 10:53 ` Peter Stephenson
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2016-04-04 10:53 UTC (permalink / raw)
  To: Cuong Manh Le, Zsh hackers list

On Mon, 04 Apr 2016 17:27:30 +0700
Cuong Manh Le <cuong.manhle.vn@gmail.com> 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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-04-04 11:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-04 10:27 Redirection order with multios option set Cuong Manh Le
2016-04-04 10:53 ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).