declare/typeset can't set array to variable name

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

declare/typeset can't set array to variable name

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

Reply | Threaded
Open this post in threaded view
|

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

Chet Ramey
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    [hidden email]    http://tiswww.cwru.edu/~chet/

Reply | Threaded
Open this post in threaded view
|

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

Greg Wooledge
On Fri, Jan 05, 2018 at 08:02:24AM -0500, Chet Ramey wrote:
> On 1/4/18 9:27 PM, Tim Burnham wrote:
> > tim@TimBookPro:~/ declare ${var2}="And this works"
> > tim@TimBookPro:~/ declare ${var2}=( This breaks )
> > -bash: syntax error near unexpected token `('

> It's a syntax error.

Pages that may or may not help:

http://mywiki.wooledge.org/BashWeaknesses
http://mywiki.wooledge.org/BashFAQ/006
http://mywiki.wooledge.org/BashProgramming

Reply | Threaded
Open this post in threaded view
|

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

Tim Burnham
In reply to this post by Chet Ramey
On Fri, Jan 5, 2018 at 8:02 AM, Chet Ramey <[hidden email]> 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    [hidden email]    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 | Threaded
Open this post in threaded view
|

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

Chet Ramey
On 1/5/18 2:05 PM, Tim Burnham wrote:

>
> 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.

The parser is consistent. It doesn't mark that word as a valid assignment
statement, either, which has implications for how it's expanded (for
example, it undergoes word splitting). That word doesn't contain any
metacharacters, so it's not a syntax error.  It just appears as an argument
to `declare', which knows how to deal with it.

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