From: Name withheld by request on
Bug or feature? Pls explain:

/tmp $ env -i bash -c 'x=4;bash -c "echo x: /\$x/"'
x: //
/tmp $ env -i x=9 bash -c 'x=4;bash -c "echo x: /\$x/"'
x: /4/

--snip
~ $ uname -r; echo $BASH_VERSION
2.6.27.41-170.2.117.fc10.i686
3.2.39(1)-release

--
thx
From: Michael Paoli on
On May 15, 4:52\xa0pm, anonb6e9(a)nyx3.nyx.net (Name withheld by
request) wrote:
> Bug or feature? Pls explain:

Feature

> /tmp $ env -i bash -c 'x=4;bash -c "echo x: /\$x/"'
> x: //
In the above case, x is never set in or exported to the environment,
thus when a new shell is started and x examined, it's unset.

> /tmp $ env -i x=9 bash -c 'x=4;bash -c "echo x: /\$x/"'
> x: /4/
In the example above, x is set in the environment to 9, then it's
changed
to 4, then it's examined.

Some similar examples (comments preceded by //)
// we unset x, so it's neither in the environment, nor shell
// variable/parameter
$ unset x
$ env | sed -ne 's/^x=/set to &/p'
// and one can see it's not in the environment
// using this syntax, we can set it just for the environment of the
env
// command
$ x=2 env | sed -ne 's/^x=/set to &/p'
set to x=2
// we can then see it's no longer there for subsequent invocation
$ env | sed -ne 's/^x=/set to &/p'
// now we set it as shell variable/parameter
$ x=2
$ set | sed -ne 's/^x=/set to &/p'
set to x=2
// we can see it's set as shell variable/parameter, but not present in
// the environment
$ env | sed -ne 's/^x=/set to &/p'
// now we export it to the environment
$ export x
// and we can now see it's in the environment
$ env | sed -ne 's/^x=/set to &/p'
set to x=2
// changing it, even when it's exported to the environment, doesn't
// change it for ancestor (parent, etc.) PID(s)
$ echo $$.$x; sh -c 'echo $$.$x; x=4; echo $$.$x'; echo $$.$x
5169.2
17754.2
17754.4
5169.2
$