bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bash 4.1 doesn't behave as I think it should: arrays and the environ


From: Stephane Chazelas
Subject: Re: Bash 4.1 doesn't behave as I think it should: arrays and the environment
Date: Sun, 19 Aug 2012 20:17:07 +0100
User-agent: Mutt/1.5.21 (2010-09-15)

2012-08-17 17:18:12 -0400, Greg Wooledge:
> On Fri, Aug 17, 2012 at 03:19:56PM +0800, John Summerfield wrote:
> > In two cases I wish to pass an array in the environment, like so:
> > 14:28 john@Boomer$ STUFF[1]=one STUFFX=stuffx env | grep ^ST
> > STUFFX=stuffx
> > STUFF[1]=one
> 
> You are creating an environment variable called "STUFF[1]".  This is
> an invalid environment variable name, but env(1) receives it and
> dutifully prints it out.  However, Bash recognizes that it is an
> invalid name, and does not turn it into a usable shell variable.
[...]

Well, if it were an invalid environment variable, why would bash
pass it to env? STUFF[1] is a valid environment variable name, but
not a valid shell variable name.

> Some older versions of Bash would also strip such invalid variables
> from the environment before invoking child processes.  Bash 4.2
> leaves them in the environment because of complaints about build
> systems that were using them for some purpose.  I don't know whether
> Bash 4.1 is one that stripped them or preserved them.  In either
> case, you should not be writing programs that rely on invalid variable
> names.
[...]

It's different here. It's the shell that *sets* the STUFF[1]
env var passed to the env command, it's not about inheriting it.

Note that no other shell would put "STUFF[1]=on" in env's
environ there:

$ zsh -c 'STUFF[1]=one STUFFX=stuffx env' | grep STUFF
STUFFX=stuffx
$ ksh -c 'STUFF[1]=one STUFFX=stuffx env' | grep STUFF
STUFFX=stuffx
$ pdksh -c 'STUFF[1]=one STUFFX=stuffx env' | grep STUFF
STUFFX=stuffx
$ ash -c 'STUFF[1]=one STUFFX=stuffx env' | grep STUFF
ash: 1: STUFF[1]=one: not found
$ bourne-sh -c 'STUFF[1]=one STUFFX=stuffx env' | grep STUFF
bourne-sh: STUFF[1]=one: not found

~$ ksh -c 'STUFF[0]=one STUFFX=stuffx env' | grep STUFF
STUFF=one
STUFFX=stuffx
~$ pdksh -c 'STUFF[0]=one STUFFX=stuffx env' | grep STUFF
STUFF=one
STUFFX=stuffx
~$ bash -c 'STUFF[0]=one STUFFX=stuffx env' | grep STUFF
STUFF[0]=one
STUFFX=stuffx


(in ksh, $V is short for ${V[0]}).

-- 
Stephane





reply via email to

[Prev in Thread] Current Thread [Next in Thread]