Control: tags -1 wontfix
On Mon, 26 Dec 2005 09:39:07 +0000 mr_platelet+m689
<mr_platelet+m...@fastmail.fm> wrote:
I would expect the following command to always give the same output,
because I seed RANDOM at the outset:
bash3 -c 'RANDOM=1; set -x; true $RANDOM; true $RANDOM | true $RANDOM'
What I find in practise, however, is that, although the first line
printed is always the same, the next 2 lines are different on each run.
My guess is that a subshell is being created to run the pipeline, and
that bash, as part of its initialisation code for a subshell, seeds
RANDOM again, wiping out the seed I specified.
Bash holds its promise to provide a stable output once RANDOM is seeded
inside the same shell:
$ cmd='RANDOM=1; set -x; true $RANDOM; true $RANDOM | true $RANDOM;
true $RANDOM; true $RANDOM'
$ diff -u <(bash -c "$cmd" 2>&1) <(bash -c "$cmd" 2>&1)
--- /dev/fd/63
+++ /dev/fd/62
@@ -1,5 +1,5 @@
+ true 16807
-+ true 20358
-+ true 31354
++ true 8403
++ true 13062
+ true 10791
+ true 19566
As you noted, the evaluation of pipelines is a bit surprising, but the
documentation clearly states:
> Each command in a multi-command pipeline, where pipes are created, is
executed in a subshell, which is a separate process.
Exporting RANDOM does not help, but explicitly setting RANDOM inside
each subshell fixes this issue:
$ cmd='RANDOM=1; set -x; true $RANDOM; { RANDOM=3; true $RANDOM;} |
{ RANDOM=7; true $RANDOM;} ; true $RANDOM; true $RANDOM'
$ diff -u <(bash -c "$cmd" 2>&1) <(bash -c "$cmd" 2>&1) && echo same
same
Regards,
--
Gioele Barabucci