title: How to create a map of key:array in shell?
body: <p>I want to create map in shell. Where each value is an array. So the map is key:array pair. For example it can be like this :</p>
  
  <pre><code>"Key1" : a1 a2 a3 a4
  "key2" : b1 b2 b3
  "key3" : c1
  </code></pre>
  
  <p>basically my code looks like this </p>
  
  <pre><code>listService(){
  serviceType=$1
  servicesList=($(getServices $serviceType))
  }
  
  listService serviceTypeA
  listService serviceTypeB
  listService serviceTypeC
  </code></pre>
  
  <p>here <code>getServices</code> is a function which returns an array of services based on the argument passed as <code>$serviceType</code>. So every time i call the <code>listService</code> function my <code>serviceList</code> gets overridden by new service list. But I want to keep all the services from different service type in form of a map like this :</p>
  
  <pre><code>"serviceA" : a1 a2 a3 a4
  "serviceB" : b1 b2 b3
  "serviceC" : c1
  </code></pre>
  
  <p>After that I want to access each array based on the key. How to achieve this. </p>
  
  <p>Thanks in advance for your help.</p>
  
  <p>Edit : I tried the answer provided by @cdarke . Here is my code now :</p>
  
  <pre><code>#!/bin/bash
  declare -A arrayMap
  
  getValues(){
    key=$1
    case $key in
      AAA )
      arr=( AA AAA AAAA )
        ;;
      BBB )
      arr=( BB BB BBBB )
        ;;
      CCC )
      arr=()
      ;;
      esac
      echo "${arr[@]}"
  }
  
  fillArrayMap(){
    param=$1
    values=( $(getValues $param) )
    printf "\nIn $param\n"
    echo -e "\nArray values is: ${values[@]}\n"
    printf "\nLength of the array values is : ${#values[@]}\n"
    arrayMap["$param"]=$values #THIS IS THE KEY LINE
    valuesList=${arrayMap[$param]} 
    echo -e "\nArray valuesList is: ${valuesList[@]}\n"
    printf "\nLength of the array valuesList is : ${#valuesList[@]}\n"
  }
  
  fillArrayMap AAA
  fillArrayMap BBB
  fillArrayMap CCC
  </code></pre>
  
  <p>Now from output I can see <code>valuesList</code> is getting only the first element of the <code>values</code> array. But I want <code>valuesList</code> to contain all the elements returned by the method <code>getValues</code>. i.e </p>
  
  <pre><code>valuesList= ${arrayMap[$param]}
  </code></pre>
  
  <p>now <code>valuesList</code> should contain all the elements, instead now it contains only 1 element. How to fix that ?</p>
  
  <p>Note: My goal is to access each individual element like AAA or AA, I don't need it as a whole as a string like AA AAA AAAA</p>