bug-bash
[Top][All Lists]
Advanced

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

Re: Funny behaviour of associative arrays


From: Dennis Williamson
Subject: Re: Funny behaviour of associative arrays
Date: Tue, 27 Jun 2023 06:24:30 -0500

On Tue, Jun 27, 2023 at 12:29 AM n952162 <n952162@web.de> wrote:

> Is this correct?
>
>     declare -A l1
>
>     l1=([a]=b [c]=d)
>     echo ${!l1[@]}
>
>     l1=($(echo [a]=b [c]=d))
>     echo ${!l1[@]}
>
> $ bash  t4
> c a
> [a]=b [c]=d
>
> If so, why?  And how can I assign a list of members to an associative
> array?
>

What is t4? Is that a script that contains the lines above?

Your first assignment is a way to assign a list of members to an
associative array. Your second assignment creates a single element with the
index "[a]=b [c]=d" which has a null value.

If you want to see the structure (keys and values) of an array or see the
values of any variable, use declare -p for the most clarity.

    $ declare -A l1

    $ l1=([a]=b [c]=d).   # assignment of a list of keys and values
    $ declare -p  l1
    declare -A l1=([c]="d" [a]="b" )

    $ l1=($(echo [a]=b [c]=d)).   # not what you want
    $ declare -p  l1
    declare -A l1=(["[a]=b [c]=d"]="" )

-- 
Visit serverfault.com to get your system administration questions answered.


reply via email to

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