Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: RANDOM shell expansion
- X-seq: zsh-workers 54070
- From: Mikael Magnusson <mikachu@xxxxxxxxx>
- To: "Mark J. Reed" <markjreed@xxxxxxxxx>
- Cc: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>, Philippe Altherr <philippe.altherr@xxxxxxxxx>, Luis Estrozi <lestrozi@xxxxxxxxx>, zsh-workers@xxxxxxx
- Subject: Re: RANDOM shell expansion
- Date: Wed, 12 Nov 2025 06:59:52 +0100
- Archived-at: <https://zsh.org/workers/54070>
- In-reply-to: <CAA=-s3y=s1mX4fhGjaOM24tTuRs2hNe6YC=06zB+CdHPJdumrw@mail.gmail.com>
- List-id: <zsh-workers.zsh.org>
- References: <CAFy7FXim5vU+ny70+VLoMQ1TJjydw6-mY212F6EuuYcpZB_Y=g@mail.gmail.com> <CAGdYchv_EbF6cz8uaLH-naSqne3C58zdWunTnyjW+tYqy=txpQ@mail.gmail.com> <CAH+w=7aNsBW4e+ek5K_yJcPYGGYH+HBxutUL2biBMW1jN2Ub6A@mail.gmail.com> <CAA=-s3y=s1mX4fhGjaOM24tTuRs2hNe6YC=06zB+CdHPJdumrw@mail.gmail.com>
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