bug-bash
[Top][All Lists]
Advanced

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

Re: Unexpected result of array assignment


From: Darren 'Tadgy' Austin
Subject: Re: Unexpected result of array assignment
Date: Thu, 18 Jul 2019 09:12:23 +0100
User-agent: Alpine 2.21 (LNX 202 2017-01-01)

Hi,
  Thanks for the quick reply. :)

[I tried to cc bash@tlinx.org on this email, but get a SERVFAIL from both DNS 
servers, so couldn't copy you in directly, sorry!]

On Wed, 17 Jul 2019, L A Walsh wrote:
> 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" )

Indeed.  I found a couple of ways of achieving what I wanted, using the += 
operator being one of them - but this is only available in bash 4.4+ and I 
need to support older versions (back to 4.0).

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

But the 'wipe before assignment' is inconsistent with how bash handles any 
other assignment.  For example:
  FOO=bar
  FOO="$bar baz"
will result in FOO = "bar baz", not simply " baz" as happens with the array 
assignment.

I can work around the issue using a different syntax, but I thought it might 
be worth reporting the inconsistency :)

Cheers,
Darren.



reply via email to

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