bug-bash
[Top][All Lists]
Advanced

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

Re: associative array assignment from the output of a function


From: Axel
Subject: Re: associative array assignment from the output of a function
Date: Fri, 22 Oct 2010 16:49:35 +0200
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.2.11) Gecko/20101013 Lightning/1.0b2 Thunderbird/3.1.5

Le 22/10/2010 16:21, Greg Wooledge a écrit :
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.

Ok I see what you mean, finally it works only for indexed arrays without explicit indexes. I understand that point.

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]}"
baz
imadev:~$ echo "${array[54]}"
blah.txt
imadev:~$ echo address@hidden
901

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.


Thanks again for those advices.
Regards




reply via email to

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