declare/typeset can't set array to variable name

2018-01-05 Thread Tim Burnham
Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: darwin17.0.0
Compiler: clang
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64'
-DCONF_OSTYPE='darwin17.0.0'
-DCONF_MACHTYPE='x86_64-apple-darwin17.0.0' -DCONF_VENDOR='apple'
-DLOCALEDIR='/usr/local/Cellar/bash/4.4.12/share/locale'
-DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H -DMACOSX   -I.  -I.
-I./include -I./lib -I./lib/intl
-I/private/tmp/bash-20170916-80428-1663r/bash-4.4/lib/intl
-DSSH_SOURCE_BASHRC -Wno-parentheses -Wno-format-security
uname output: Darwin TimBookPro.local 17.3.0 Darwin Kernel Version
17.3.0: Thu Nov  9 18:09:22 PST 2017;
root:xnu-4570.31.3~1/RELEASE_X86_64 x86_64
Machine Type: x86_64-apple-darwin17.0.0

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 `('



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

2018-01-05 Thread Tim Burnham
On Fri, Jan 5, 2018 at 8:02 AM, Chet Ramey  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, CWRUc...@case.eduhttp://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.