[Top][All Lists]

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

Re: Unexpected result of array assignment

From: L A Walsh
Subject: Re: Unexpected result of array assignment
Date: Wed, 17 Jul 2019 22:46:00 -0700
User-agent: Thunderbird

On 2019/07/17 18:16, Darren 'Tadgy' Austin wrote:
> Repeat-By:
>         declare -A foo
>         foo=(["key"]="value1")
>         declare -p foo
>         foo=(["key"]="${foo["key"]} value2")
>         declare -p foo
>         The above should result in 'foo["key"]' having a value of 'value1 
> value2', but the result is simply ' value2', which I believe to be incorrect 
> behaviour.

In bash4.4.12, Using:

I think you need to tell bask that you are updating 'foo'
instead of assigning to it:

This seems to do what you want:

foo+=([key]="${foo[key]} value2")
> my -p foo
declare -A foo=([key]="value1 value2" )

or w/quotes:
foo+=(["key"]="${foo["key"]} value3")

> my -p foo
declare -A foo=([key]="value1 value2 value3" )

I think that without the update it becomes an assign and clears
the value assigned to 'key' before using it to form the string.

It certainly isn't intuitive, but I don't know if there is a
guarantee of it picking up the value of "foo[key]" before initializing
the target space.

reply via email to

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