bug-bash
[Top][All Lists]
Advanced

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

Re: declare/typeset can't set array to variable name


From: Tim Burnham
Subject: Re: declare/typeset can't set array to variable name
Date: Fri, 5 Jan 2018 14:05:43 -0500

On Fri, Jan 5, 2018 at 8:02 AM, Chet Ramey <chet.ramey@case.edu> wrote:
> On 1/4/18 9:27 PM, Tim Burnham wrote:
>
>> Bash Version: 4.4
>> Patch Level: 12
>> Release Status: release
>>
>> Description:
>> Declare/typeset throws error when trying to create a new array to a
>> name held in a variable.
>>
>> Repeat-By:
>> tim@TimBookPro:~/ declare var1=( This works )
>> tim@TimBookPro:~/ var2=var3
>> tim@TimBookPro:~/ declare ${var2}="And this works"
>> tim@TimBookPro:~/ declare ${var2}=( This breaks )
>> -bash: syntax error near unexpected token `('
>> tim@TimBookPro:~/ declare -a ${var2}=( array flag doesnt matter )
>> -bash: syntax error near unexpected token `('
>
> It's a syntax error.  `declare' is a `declaration command', as Posix
> terms them, and takes assignment statements as arguments. If the parser
> can detect an argument to `declare' as a valid assignment statement, it
> will allow syntax, such as compound assignments, that it allows for
> standalone assignments.  In this case, the `${var2}' on the left side
> of the assignment renders that word an invalid assignment statement,
> since `${var2}' is not a valid identifier. Because it's not an assignment
> statement, the left paren is not allowed to begin a compound assignment
> and is treated as the operator it usually is. This isn't a place where the
> grammar allows a left paren, so it's a syntax error.
>
> Chet
> --
> ``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/

Why does the parser inconsistently detect the identifier `${var2}'? In
the 3rd line of my example, Bash assigns the given expression into
`${var3}' as intended.



reply via email to

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