[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: declare/typeset can't set array to variable name
From: |
Chet Ramey |
Subject: |
Re: declare/typeset can't set array to variable name |
Date: |
Fri, 5 Jan 2018 08:02:24 -0500 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 |
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/