Skip to content

Instantly share code, notes, and snippets.

@mike0004
Last active October 16, 2015 14:16
Show Gist options
  • Save mike0004/6a8afc4ad8a2b066e38a to your computer and use it in GitHub Desktop.
Save mike0004/6a8afc4ad8a2b066e38a to your computer and use it in GitHub Desktop.
associative array example, requires bash 4
#!/usr/bin/env bash
# good ref: http://www.artificialworlds.net/blog/2012/10/17/bash-associative-array-examples/
# requires bash 4 for associative arrays
declare -A MYMAP=(
[foo]=bar
[baz]=quux
)
KEYS=(${!MYMAP[@]}) # Make a normal array containing all the keys in the associative array
for (( I=0; $I < ${#MYMAP[@]}; I+=1 )); do
KEY=${KEYS[$I]}
echo $KEY --- ${MYMAP[$KEY]};
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment