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: Lawrence Velázquez
Subject: Re: Issue declaring an array via a variable name
Date: Sat, 21 Aug 2021 21:30:38 -0400
User-agent: Cyrus-JMAP/3.5.0-alpha0-1118-g75eff666e5-fm-20210816.002-g75eff666

On Sat, Aug 21, 2021, at 6:02 PM, Hunter Wittenborn wrote:
> In my head, something like this (where 'value' is equal to 'y'):
> 
> `declare "${value}"="x"`
> 
> becomes this (which it appears to do so):
> 
> `declare "y"="x"`

Almost.  The argument parses without issue ('=' has no special
meaning here), so it undergoes the usual parameter expansion and
quote removal, and you end up with

    declare y=x

which is fine.

> Logically (for me anyway), this:
> 
> `declare "${value}"=("x" "z")`
> 
> should then become this:
> 
> `declare "y"=("x" "z")`

The shell performs parameter expansion and quote removal on a command
*after* parsing it.  As I see it, your mental model for `declare`
requires the shell to:

   1. Pause parsing upon seeing '('.
   2. Go back and, out of the usual order, perform parameter expansion
      and quote removal on the portion of the word preceding '=' to
      see whether it expands to a valid identifier.
  3a. If it does, resume parsing the word as an assignment, so '('
      begins an array.
  3b. If not, resume parsing as regular word, so '(' is a syntax error.

These would be rules that don't apply anywhere else.  You may find
this logical and worthwhile, but others sure don't.

    "${value}"=("x" "z")          # invalid
    echo "${value}"=("x" "z")     # invalid
    declare "${value}"=("x" "z")  # invalid but you want it to be valid

As has already been pointed out, this all goes away if you just
quote the parentheses -- even if you're silly about it:

    declare -a "${value}"=\(x\ z\)

-- 
vq



reply via email to

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