title: How to combine associative arrays in bash?
body: <p>Does anyone know of an elegant way to combine two associative arrays in <code>bash</code> just like you would a normal array? Here's what I'm talking about:</p>
  
  <p>In bash you can combine two normal arrays as follows:</p>
  
  <pre><code>declare -ar array1=( 5 10 15 )
  declare -ar array2=( 20 25 30 )
  declare -ar array_both=( ${array1[@]} ${array2[@]} )
  
  for item in ${array_both[@]}; do
      echo "Item: ${item}"
  done
  </code></pre>
  
  <p>I want to do the same thing with two associative arrays, but the following code does not work:</p>
  
  <pre><code>declare -Ar array1=( [5]=true [10]=true [15]=true )
  declare -Ar array2=( [20]=true [25]=true [30]=true )
  declare -Ar array_both=( ${array1[@]} ${array2[@]} )
  
  for key in ${!array_both[@]}; do
      echo "array_both[${key}]=${array_both[${key}]}"
  done
  </code></pre>
  
  <p>It gives the following error:</p>
  
  <blockquote>
    <p>./associative_arrays.sh: line 3: array_both: true: must use subscript when assigning associative array</p>
  </blockquote>
  
  <p>The following is a work-around I came up with:</p>
  
  <pre><code>declare -Ar array1=( [5]=true [10]=true [15]=true )
  declare -Ar array2=( [20]=true [25]=true [30]=true )
  declare -A array_both=()
  
  for key in ${!array1[@]}; do
      array_both+=( [${key}]=${array1[${key}]} )
  done
  
  for key in ${!array2[@]}; do
      array_both+=( [${key}]=${array2[${key}]} )
  done
  
  declare -r array_both
  
  for key in ${!array_both[@]}; do
      echo "array_both[${key}]=${array_both[${key}]}"
  done
  </code></pre>
  
  <p>But I was hoping that I'm actually missing some grammar that will allow the one-liner assignment as shown in the non-working example.</p>
  
  <p>Thanks!</p>