Issue declaring an array via a variable name

2021-08-14 Thread Hunter Wittenborn
Hi,



I was doing some testing for some additions to a rather big Bash script I'm 
working on, and the following code kept failing whenever I attempted to run it:



"

variable="hello"



declare -g "${variable}"=("world" "me")

"



When run, it's complaining about a syntax error near "(", appearing to be on 
the 'declare' line right next to where I start typing the array.



In my testing, all of the following would fail:



- declare -g "${variable}"=("world" "me")

- declare -g ${variable}=("world" "me")

- declare -g "hello"=("world" "me")



The only one that did work was the following:



- declare -g hello=("world" "me")



This appeared to be a bug for me, but I wasn't sure if this was somehow a 
feature or not.



Lastly: A workaround for this was found here [1] which uses namerefs, and works 
just fine (and is probably easier for me to look at anyway), but this seemed 
like something that deemed a bug report regardless.



[1]: https://lists.gnu.org/archive/html/help-bash/2021-08/msg00069.html

---

Hunter Wittenborn

https://www.hunterwittenborn.com

https://github.com/hwittenborn


Re: Issue declaring an array via a variable name

2021-08-19 Thread Hunter Wittenborn
The
 general consensus seems to be that this isn't really a bug (from what 
I've understood), but it looks like I can use a variable reference to 
solve the issue nevertheless.



Regardless, 
I'm still wanting to almost classify this as a bug (or possibly a 
feature request; it feels like it's getting blurry where it would be at 
this point).







Because; Both of these work:



declare "var"="i"

declare var=("i" "j")



So, in my opinion, this should logically work (from the view of an end user):



declare "var"=("i" "j")





The same issue applies for variables, though seeming to not matter if they're 
in quotes or not for those.



Lastly
 - again, the variable reference issue is seeming to work fairly fine 
for my current use cases. I just don't really want to throw this off as a
 "syntax error" when this quite arguably seems like a bug instead.





 On Mon, 16 Aug 2021 09:27:55 -0500 Greg Wooledge  wrote 



On Mon, Aug 16, 2021 at 04:10:31PM +0200, Alex fxmbsw7 Ratchev wrote: 
> i dunno but, i had many testings many times 
> the stable wanted is to enclose the per arg definition in quotes, not like 
> "var"="var" 
> the right dash options is also useful 
 
I don't understand this paragraph at all. 
 
> test=( a b ) t=var 
> declare -ga "$t=( ${test@Q} )" 
 
I believe this is what you're trying to show us: 
 
unicorn:~$ declare -ga "${t}=( ${test@Q} )" 
unicorn:~$ declare -ga "${t}"=( ${test@Q} ) 
bash: syntax error near unexpected token `(' 
 
And likewise, 
 
unicorn:~$ declare -ga "$t=( ${test@Q} )" 
unicorn:~$ declare -ga "$t"=( ${test@Q} ) 
bash: syntax error near unexpected token `(' 
 
There is something subtle happening there, at the parsing level.  Chet's 
explanation (quoted below) may suffice for some. 
 
For my own purposes, "if it hurts, don't do that" is good enough. 
But that's just me. 
 
> the same style i found secure and data preserving such alike: 
> "list=( $( ls -Ac --quoting-style=shell-escape ) )" 
 
That has nothing to do with the issue that started this thread.  You've 
got a plain old compound array assignment, with a command substitution 
inside it.  The variable on the left hand side ("list") is a valid 
shell identifier.  There are no surprises here. 
 
> On Mon, Aug 16, 2021, 15:57 Chet Ramey <mailto:chet.ra...@case.edu> wrote: 
> > It's a syntax error. There is an unquoted operator (`(') where the grammar 
> > does not allow it. 
> > 
> > `declare' does allow compound array assignment statements as arguments, and 
> > the parser accommodates this as long as two conditions hold: the parser can 
> > detect that the first word of a simple command is `declare' and the 
> > argument is a valid assignment statement. In this case, the second fails, 
> > since `"${variable}"' is not a valid shell identifier. That renders the 
> > argument not a valid assignment statement. 
 





---

Hunter Wittenborn

https://www.hunterwittenborn.com

https://github.com/hwittenborn


Re: Issue declaring an array via a variable name

2021-08-21 Thread Hunter Wittenborn
> As an end user I would expect the unquoted `('

> operator to cause a syntax error, just as it does in `echo ('.



Well I'm expecting '(' to be part of the shell's syntax (when unquoted; so 
likewise not cause a syntax error), but when looking at things like the left 
side of a variable assignment, I'm sure you'll agree that it should allow any 
string that fits a variable's normal specification (regardless of being an 
array or not).



Maybe this will explain how I'm thinking about it:



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"`



Logically (for me anyway), this:



`declare "${value}"=("x" "z")`



should then become this:



`declare "y"=("x" "z")`