79572116

Date: 2025-04-13 22:19:59
Score: 1.5
Natty:
Report link

@alek has it right. Just to show as an example:

declare -Ar array1=( [5]=true [10]=true [15]=true )
declare -Ar array2=( [20]=true [25]=true [30]=true )
declare -A array_both

eval "array_both=( ${array1[*]@K} ${array2[*]@K} )"

for key in ${!array_both[@]}; do
    echo "array_both[${key}]=${array_both[${key}]}"
done

produces

$ bash /tmp/test.sh 
array_both[30]=true
array_both[5]=true
array_both[25]=true
array_both[20]=true
array_both[10]=true
array_both[15]=true

The single line and the eval are key to this. I did it on single steps (in an attempt to make it clear) and it produces some very odd results (all keys and no values)

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @alek
  • Looks like a comment (1):
  • Low reputation (0.5):
Posted by: GraemeV