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

Re: broken random variable



On Sat, 19 Oct 2019 at 07:39, . . <apatiskogen@xxxxxxxxxx> wrote:
>
> Hello, zsh-workers
>
> I found a bug in the $RANDOM variable; on OpenBSD, the command "echo $RANDOM | tee" will print the same number
> across different executions of the same command, and the value of "echo $RANDOM" will be the same as the last value.

That's because Zsh is different from other shells in one regard: for the pipe:

A | B

it runs A in a subshell and B in the current shell, while other shells
like Bash run A in the current shell and B in the subshell. This is a
very nice feature, as it allows to do:

cat /some/file | while read line; do some_var=…; done
print $some_var

and the side-effects of this pipe will survive to the `print'.

So, the `echo $RANDOM | tee` will print the same value because the
$RANDOM will be read in a subshell, meaning that a fork() will be done
and the random-seed will be left unchanged in the outer process.

-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org



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