[Top][All Lists]

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

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

From: John Summerfield
Subject: Bash 4.1 doesn't behave as I think it should: arrays and the environment
Date: Fri, 17 Aug 2012 15:19:56 +0800
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.6esrpre) Gecko/20120717 Thunderbird/10.0.6

GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

I am running on 64-bit CentOS 6.

I have been writing a script that reads a text file and munges it to create a shell command to run a script.

In two cases I wish to pass an array in the environment, like so:
14:28 address@hidden STUFF[1]=one STUFFX=stuffx env | grep ^ST
14:28 address@hidden

Of course, my script is not called "env," but this shows the sort of thing that is clearly visible within my script.

The symptom I observed is that the elements of STUFF are visible in the environment, but expand to NULL in my script. However, things like STUFFX are visible in the environment and their values are accessible in the script.

The man page for bash contains a para entitled ENVIRONMENT which doesn't mention arrays, leaving the reader to assume they are not different from other shell variables.

It was some time (hours) before I tried this:
14:28 address@hidden export STUFF[1]=one
bash: export: `STUFF[1]': not a valid identifier
14:33 address@hidden

I have found no documentation (and I have searched http://www.gnu.org/software/bash/manual/bashref.html ) that clarifies what is going on.

Now, passing arrays via the environment is useful, I found a use for it.

It is possible (more or less) to get elements of the array into the environment. It's unclear to me, but I suppose the above example may have created a variable "STUFF[1]."

I note that IEEE Std 1003.1, 2004 Edition 6 allows implementations to expand the base rules regarding variable names so that "STUFF[1]" is permissible in the environment.

I suggest bash be enhanced to allow the export statement, or something analogous applying to arrays, so that array elements can be passed in the same way as ordinary variables.

If there exists a way to do what I have been trying, then it needs to be documented somewhere and mentioned in the ENVIRONMENT para and in documentation of the set command and arrays, and maybe other places too.

John Summerfield

reply via email to

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