[Top][All Lists]

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

Re: associative array assignment from the output of a function

From: Greg Wooledge
Subject: Re: associative array assignment from the output of a function
Date: Fri, 22 Oct 2010 10:21:07 -0400
User-agent: Mutt/

On Fri, Oct 22, 2010 at 02:59:37PM +0200, Axel wrote:
> Anyway, since this syntax works for assigning indexed arrays (even if 
> it's a "hack"), should it works for associative array assignment ? 
> Maintainers will decide I assume.

But it doesn't.  You used a totally different syntax for your indexed
array example.  If you had used the same syntax, it would have looked
like this:

imadev:~$ unset array
imadev:~$ f() { echo '[0]=alpha [2]=beta'; }; array=( $(f) )
imadev:~$ set | grep ^array=
array=([0]="[0]=alpha" [1]="[2]=beta")

It doesn't "work" in the indexed array case either.

There's another hazard you haven't taken into account (although it's
only a hazard when dealing with general strings; numbers are safe).
The unquoted substitution $(f) is subject to both word-splitting (which
you've been taking advantage of) *and* globbing.

imadev:~$ f() { echo '+ - * /'; }; array=( $(f) )
imadev:~$ echo "${array[42]}"
imadev:~$ echo "${array[54]}"
imadev:~$ echo address@hidden

If you're going to use array=( $... ) with arbitrary strings being
possible in the substitution, then you should disable globbing first
(with set -f) to avoid potential disasters.

reply via email to

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