[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: the context of expansion for output redirection
From: |
Chet Ramey |
Subject: |
Re: the context of expansion for output redirection |
Date: |
Tue, 29 Sep 2009 12:08:10 -0400 |
User-agent: |
Thunderbird 2.0.0.23 (Macintosh/20090812) |
Lewis Hyatt wrote:
> Hello-
>
> I was somewhat surprised to see the following behavior (this is 3.1.17):
>
> $ x=0; /bin/echo > /tmp/$((++x)); echo $x
> 0
> $ x=0; echo > /tmp/$((++x)); echo $x
> 1
>
> I guess in the first case the expansion of $((++x)) is taking place after
> forking, whereas in the second case there is no forked process so the
> expansion
> happens in the context of the current shell. I would have thought that both
> cases would output "1", though. Is this expected or documented anywhere?
> Thanks
> very much.
The Bash philosophy has always been that redirections are carried out in
the same context as the command they are part of. That means that a
command that causes a subshell to be created will have its redirections
performed in that subshell environment. Similarly for builtin commands
in the current shell context.
Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU chet@case.edu http://cnswww.cns.cwru.edu/~chet/