#!/bin/bash # # associative_array.bash. Derived from: # https://stackoverflow.com/questions/3112687/how-to-iterate-over-associative-arrays-in-bash # Make an array and stuff it full of values declare -A array array[repo_one]=true array[repo_two]=true array[repo_three]=false # The key is accessed using the exclamation point for i in "${!array[@]}" do echo "key: $i" if ${array[$i]}; then echo "$i is true" fi done # Output will be: # # key: repo_one # repo_one is true # key: repo_two # repo_one is true # key: repo_three # NOTE: Be care of -A versus -a in the array declaration. A -a makes an *indexed* array as opposed to an *associative* array. # But given that an associative array is actually a hash table, it doesn't store the pairs in the order you put them # into the array, they are instead stored in their hash value order. If order matters you need to use an indexed array. # The following shows how to use both an indexed array and an associative array together to get both, if that's what you need. # (Ref https://stackoverflow.com/questions/29161323/how-to-keep-associative-array-order) declare -a arrOrder ; declare -A arrData arrOrder+=( repo_one ) ; arrData[repo_one]=true arrOrder+=( repo_two ) ; arrData[repo_two]=true arrOrder+=( repo_three ) ; arrData[repo_three]=false for i in "${arrOrder[@]}" do echo "$i: ${arrData[$i]}" done