Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.unix.shell > #4929

Re: bash: Initialisation of an associative array using a compound assignment

From Stephane Chazelas <stephane.chazelas@gmail.com>
Newsgroups comp.unix.shell
Subject Re: bash: Initialisation of an associative array using a compound assignment
Date 2012-05-06 17:16 +0100
Organization A noiseless patient Spider
Message-ID <20120506161614.GA5504@chaz.gmail.com> (permalink)
References <0d56e766-9a3f-45ec-a8e9-b212375cf035@gn8g2000vbb.googlegroups.com>

Show all headers | View raw


2012-05-04 01:43:28 -0700, Francis Moreau:
> Hello,
> 
> I don't understand why initialising an associative array like the
> following doesn't work:
> 
>   $ declare -A a
>   $ a=($(echo '[key]=val')
>   bash: a: [key]=val: must use subscript when assigning associative
> array
> 
> Could anybody enlight me ?
[...]

Note that some old versions of bash (though those were before
bash had associative arrays) had a bug
(http://lists.gnu.org/archive/html/bug-bash/2003-10/msg00065.html)
in that

a=($(echo '[1]=2'))

would assign  2 to a[1] instead of [1]=2 to a[0].

So you couldn't reliably do

a=(*)

or 

a=($(some-cmd))

unless you could guarantee that none of the words resulting from
word splitting and filename generation had the form [n]=v.

This has been fixed since.

Note that the [x]=foo syntax comes from ksh93, just like most
things awkward with shell scripting...

-- 
Stephane

Back to comp.unix.shell | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

bash: Initialisation of an associative array using a compound assignment Francis Moreau <francis.moro@gmail.com> - 2012-05-04 01:43 -0700
  Re: bash: Initialisation of an associative array using a compound assignment "Chris F.A. Johnson" <cfajohnson@gmail.com> - 2012-05-04 14:24 -0400
    Re: bash: Initialisation of an associative array using a compound assignment Barry Margolin <barmar@alum.mit.edu> - 2012-05-04 19:15 -0400
      Re: bash: Initialisation of an associative array using a compound assignment Janis Papanagnou <janis_papanagnou@hotmail.com> - 2012-05-05 09:42 +0200
        Re: bash: Initialisation of an associative array using a compound assignment Barry Margolin <barmar@alum.mit.edu> - 2012-05-05 17:43 -0400
          Re: bash: Initialisation of an associative array using a compound assignment Janis Papanagnou <janis_papanagnou@hotmail.com> - 2012-05-06 03:41 +0200
            Re: bash: Initialisation of an associative array using a compound assignment Barry Margolin <barmar@alum.mit.edu> - 2012-05-05 22:34 -0400
        Re: bash: Initialisation of an associative array using a compound assignment Francis Moreau <francis.moro@gmail.com> - 2012-05-06 07:30 -0700
      Re: bash: Initialisation of an associative array using a compound assignment Francis Moreau <francis.moro@gmail.com> - 2012-05-06 07:35 -0700
      Re: bash: Initialisation of an associative array using a compound assignment Francis Moreau <francis.moro@gmail.com> - 2012-05-08 00:57 -0700
  Re: bash: Initialisation of an associative array using a compound assignment Stephane Chazelas <stephane.chazelas@gmail.com> - 2012-05-06 17:16 +0100
    Re: bash: Initialisation of an associative array using a compound assignment Janis Papanagnou <janis_papanagnou@hotmail.com> - 2012-05-07 02:06 +0200

csiph-web