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

Re: funny subshell effect



On Thu, Mar 10, 2011 at 12:18 AM, Mikael Magnusson <mikachu@xxxxxxxxx> wrote:
> This confused me for a few minutes, I was trying to play a random midi file with
> pmidi *(oe:REPLY=\$RANDOM:[1])
> but it plays the same one each time, but when i tried
> echo *(oe:REPLY=\$RANDOM:[1])
> it printed a different one each time
>
> % repeat 3; do echo .(e:REPLY=\$RANDOM:); done
> 17
> 25549
> 6369
> % repeat 3; do command echo .(e:REPLY=\$RANDOM:); done
> 5801
> 5801
> 5801
>
> Is this something that must be so? (My guess is yes, but it can't hurt to ask).
> (I know I can work around it easily by assigning to a var first).

I recently came up with a nicer workaround than making an explicit
variable, which is
() { pmidi $@ } *(oe:REPLY=\$RANDOM:[1])
for my original command in the thread. Since anonymous functions are
"builtin", the glob happens in the parent shell instead of the forked
subshell that runs pmidi, and everyone is (or at least I am) happy.

-- 
Mikael Magnusson



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