zsh-workers
 help / color / mirror / code / Atom feed
From: Sebastian Gniazdowski <sgniazdowski@gmail.com>
To: ". ." <apatiskogen@yandex.com>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: broken random variable
Date: Sat, 19 Oct 2019 11:48:28 +0200	[thread overview]
Message-ID: <CAKc7PVCNfSXJ1GhenP=umg+ZeDpteL=bQPuwpuKOYJm+9tSTJg@mail.gmail.com> (raw)
In-Reply-To: <8724321571463531@myt3-a2a42ac8caa6.qloud-c.yandex.net>

On Sat, 19 Oct 2019 at 07:39, . . <apatiskogen@yandex.com> 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

  parent reply	other threads:[~2019-10-19  9:49 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-19  5:38 
2019-10-19  5:44 ` Roman Perepelitsa
2019-10-19  6:09   ` Daniel Shahaf
2019-10-19  9:48 ` Sebastian Gniazdowski [this message]
2019-10-19 14:31 ` Mikael Magnusson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAKc7PVCNfSXJ1GhenP=umg+ZeDpteL=bQPuwpuKOYJm+9tSTJg@mail.gmail.com' \
    --to=sgniazdowski@gmail.com \
    --cc=apatiskogen@yandex.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).