bug-bash
[Top][All Lists]
Advanced

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

Re: How to initialize a read-only, global, associative array in Bash?


From: Chet Ramey
Subject: Re: How to initialize a read-only, global, associative array in Bash?
Date: Fri, 30 Nov 2012 15:45:32 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:16.0) Gecko/20121010 Thunderbird/16.0.1

On 11/26/12 4:45 PM, Tim Friske wrote:
> Hi folks,
> 
> I execute the following code in Bash version "GNU bash, Version
> 4.2.39(1)-release (x86_64-redhat-linux-gnu)":
> 
> function foobar {
>   declare -rgA FOOBAR=([foo]=bar)
> }
> foobar
> declare -p FOOBAR
> # Output: declare -Ar FOOBAR='()'
> 
> Why doesn't Bash initialize FOOBAR with ([foo]=bar) according to
> declare -p?

Thanks for the report.  Bash does things internally so that the above
statement is executed as if it were

        declare -Ag FOOBAR
        FOOBAR=([foo]=bar)
        declare -r FOOBAR

The problem was with the code that simulated the first statement, which
did not correctly handle the `-g' option.  There are actually two variables
created: one function-local with the correct value (but not all the right
attributes), and one global with no value, but with the correct attributes.
This will be fixed in the next version.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/



reply via email to

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