bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Changing the way bash expands associative array subscripts


From: Greg Wooledge
Subject: Re: Changing the way bash expands associative array subscripts
Date: Tue, 16 Mar 2021 08:07:23 -0400

On Tue, Mar 16, 2021 at 09:24:01AM +0100, Alex fxmbsw7 Ratchev wrote:
> it doesnt make me much sense that unset -v assoc[$key] gives syntax error
> or so..

Think of it this way: what would happen if you had a filename in a
variable -- say, $file -- and you ran this command:

rm -f $file

You'd expect that to fail, right?  Of course.

unset -v assoc[$key] fails for exactly the same reason.  unset does not
have any magical powers.  It's only a shell builtin, NOT a shell keyword.
Therefore, the standard shell rules apply.  Expansions occur first,
followed by word splitting, which cause a list of argument words to be
created and passed as arguments to the builtin.

key='two words'
unset -v assoc[$key]

==> 'unset' '-v' 'assoc[two' 'words]'

You can even use set -x to see this in action:

unicorn:~$ key='two words'
unicorn:~$ set -x
unicorn:~$ unset -v assoc[$key]
+ unset -v 'assoc[two' 'words]'
bash: unset: `assoc[two': not a valid identifier
bash: unset: `words]': not a valid identifier



reply via email to

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