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

Re: RANDOM shell expansion



On Wed, Nov 12, 2025 at 1:15 AM Mark J. Reed <markjreed@xxxxxxxxx> wrote:
>
> > subshells that reference RANDOM will result in identical
>      pseudo-random values unless the value of RANDOM is referenced or
>      seeded in the parent shell in between subshell invocations.
>
> To wit:
>
> for i in {1..3}; do (echo $RANDOM) done
> 17530
> 17530
> 17530
>
> vs:
>
> for i in {1..3}; do echo $RANDOM; done
> 17530
> 12664
> 23361
>
> That all makse sense to me. But it still feels weird that when you do something like (my command) >$foo, the expansion of $foo is done in the subshell, even though it's not inside the parentheses.

As I think I mentioned in the other thread, you can use { } to force
redirects in the current shell:
% for i in {1..3}; do /bin/echo > /nonexist/$RANDOM; done
zsh: no such file or directory: /nonexist/10749
zsh: no such file or directory: /nonexist/10749
zsh: no such file or directory: /nonexist/10749
% for i in {1..3}; do { /bin/echo } > /nonexist/$RANDOM; done
zsh: no such file or directory: /nonexist/32322
zsh: no such file or directory: /nonexist/9850
zsh: no such file or directory: /nonexist/9812

-- 
Mikael Magnusson




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