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

Re: RANDOM shell expansion



Something similar was recently discussed in users/30385. The reason for the difference is that Zsh evaluates the redirection in the process of the affected command. echo and false are builtins. They are run and their redirections are evaluated in the running Zsh process. timeout is an external command. It is run and its redirection is evaluated in a forked process.

local -i i=0;      echo foo >/tmp/aaa-$((i++));      echo foo >/tmp/aaa-$((i++)) # Creates two files: aaa-0 and aaa-1
local -i i=0; /bin/echo foo >/tmp/bbb-$((i++)); /bin/echo foo >/tmp/bbb-$((i++)) # Creates a single file: bbb-0

Philippe



On Tue, Nov 11, 2025 at 11:28 AM Luis Estrozi <lestrozi@xxxxxxxxx> wrote:
Hi there

I'm new here and I'm not a subscriber, just wanted to report something funny I stumbled upon on zsh -f (v5.9).

$ for i in {1..9}; do timeout 1 sleep 2 >"/tmp/testrandzshf-${i}-${RANDOM}"; done
$ ls /tmp/testrandzshf-*/tmp/testrandzshf-1-10912 /tmp/testrandzshf-2-10912 /tmp/testrandzshf-3-10912 /tmp/testrandzshf-4-10912 /tmp/testrandzshf-5-10912 /tmp/testrandzshf-6-10912 /tmp/testrandzshf-7-10912 /tmp/testrandzshf-8-10912 /tmp/testrandzshf-9-10912

Not very random :)

Notice that:
1. The expansion is happening on each iteration ($i is correct);
2. Trying with "echo" works - which makes me think it only fails for commands that exit code > 0;
3. However, it also works with "false".

It seems to me even if *just the first iteration* exits with exitcode > 0, the problem happens.

I tried this on bash and it works as expected.

-- Luis Estrozi


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