[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Funny behaviour of associative arrays
From: |
Greg Wooledge |
Subject: |
Re: Funny behaviour of associative arrays |
Date: |
Tue, 27 Jun 2023 07:44:56 -0400 |
On Tue, Jun 27, 2023 at 06:24:30AM -0500, Dennis Williamson wrote:
> Your first assignment is a way to assign a list of members to an
> associative array. Your second assignment creates a single element with the
> index "[a]=b [c]=d" which has a null value.
I'm seeing some parser voodoo here.
unicorn:~$ string='[a]=b [c]=d'
unicorn:~$ a=( $string )
unicorn:~$ declare -p a
declare -A a=(["[a]=b [c]=d"]="" )
But...
unicorn:~$ s1='[a]=b' s2='[c]=d'
unicorn:~$ a=( $s1 $s2 )
unicorn:~$ declare -p a
declare -A a=(["[a]=b"]="[c]=d" )
So, no word splitting is performed on $string? That's surprising. Let's
see if there's globbing.
unicorn:~$ string='*.txt'
unicorn:~$ a=( $string )
unicorn:~$ declare -p a
declare -A a=(["*.txt"]="" )
Again, a surprise (although not as big a surprise if we already knew there
is no word splitting). This is entirely different from the behavior we
see when assigning to an *indexed* array:
unicorn:~$ i=( $string )
unicorn:~$ echo "${#i[@]}"
47
I'm not saying this is wrong... just surprising. Was it an intentional
decision?