bug-bash
[Top][All Lists]
Advanced

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

Re: Issue declaring an array via a variable name


From: Chet Ramey
Subject: Re: Issue declaring an array via a variable name
Date: Mon, 16 Aug 2021 09:58:59 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0) Gecko/20100101 Thunderbird/78.13.0

On 8/14/21 8:45 PM, Hunter Wittenborn wrote:
> Hi,
> 
> 
> 
> I was doing some testing for some additions to a rather big Bash script I'm 
> working on, and the following code kept failing whenever I attempted to run 
> it:
> 
> 
> 
> "
> 
> variable="hello"
> 
> 
> 
> declare -g "${variable}"=("world" "me")

Here's the answer I gave on help-bash:

It's a syntax error. There is an unquoted operator (`(') where the grammar
does not allow it.

`declare' does allow compound array assignment statements as arguments, and
the parser accommodates this as long as two conditions hold: the parser can
detect that the first word of a simple command is `declare' and the
argument is a valid assignment statement. In this case, the second fails,
since `"${variable}"' is not a valid shell identifier. That renders the
argument not a valid assignment statement.

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



reply via email to

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