2

I want to have a global associative array, that is filled at several locations and I can not get it to work to initialize the array with the content of a string without using the declare -A -g over and over at said locations(which I don't feel like is the smartest approach). I've extracted the issue to the code below:

#!/bin/bash

declare -A arr
declare inputString

inputString="([key]=val)"

arr="$inputString"
printf "from variable: ${arr[*]}\n"
printf "from variable: ${arr[key]}\n"

arr=([key]=val)
printf "inline: ${arr[*]}\n"
printf "inline: ${arr[key]}\n"

declare -A arr=$inputString
printf "with declare: ${arr[*]}\n"
printf "with declare: ${arr[key]}\n"

the output is:

from variable: ([key]=val)
from variable: 
inline: val
inline: val
with declare: val
with declare: val

whereas I would expect it to be:

from variable: val
from variable: val
inline: val
inline: val
with declare: val
with declare: val

what do I have to do to accomplish this task?

3
  • What exactly are you trying to accomplish? If you just want to be able to pass an associative array to functions, you can pass it by name and access it with ${!varname[key]} Commented Apr 13, 2023 at 13:58
  • I try to "return" an associative array from a function. As there is no returnvalue I tried to accomplish that with echoing a proper associative array string and working with this string. There is the point to get an already declared array initialized with the string. Commented Apr 13, 2023 at 14:19
  • Don't try to return an associative array, just create it as a side effect. Bash doesn't have first class arrays, you can't assign one array to another. Commented Apr 13, 2023 at 14:45

1 Answer 1

3

One bash 4.3+ approach using a nameref:

myadd() {                             # $1 == array name; $2 == key name; $3 == value
    declare -n _arr="$1"              # nameref
    _arr+=(["$2"]="$3")
}

myclear() {
    declare -n _arr="$1"
    _arr=()
}

Taking for a test drive:

unset      arr1 arr2
declare -A arr1 arr2

myadd arr1 keyX 3
myadd arr1 Index_45 16
myadd arr2 long_string_abc long_value_xyz

typeset -p arr1 arr2
    ==> declare -A arr1=([keyX]="3" [Index_45]="16" )
    ==> declare -A arr2=([long_string_abc]="long_value_xyz" )

myclear arr1

myadd arr1 aaa 111
myadd arr1 bbb 222

typeset -p arr1
    ==> declare -A arr1=([bbb]="222" [aaa]="111" )
Sign up to request clarification or add additional context in comments.

8 Comments

thank you for your thoughts. You understood that correctly, as in I have a global array I want to populate on several locations. I am not sure about the "add to" instead of "rebuild from scratch" part though. I really want to reinitialize the global array, not just adding to it. I am aware, that I can do that value by value and via the declare -A. I was just wondering if it is okay to call the declare -A -g on all those points as this feels really wrong.
@roediGERhard 'add to' vs 'rebuild from scratch' was just added to limit the scope of the answer; I've added one idea for resetting/clearing the array (myclear()); the general idea would be to issue declare -A once (in the main script) and then for the rest of the script make use of functions and namerefs to 'add to' or 'clear' the array as needed
But how do I do that without handling every value for itself? In other words: can one "fill" an array with a string? If so, how?
where is the string coming from? in one of your other comments you mention a function that echoes a string (eg, [key]=val); it sounds (to me) like the function is building the string so, would it be as 'simple' as replacing the string-building-logic with calls to the function (or duplicating the content of the myclear()/myadd() functions in your function)
oh. I understand it now. Thank you for the hint again. I guess I'll fiddle around with that and think I know what I need to do.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.