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