Bash: Correct way to Iterate over Map

Posted by Lars Tackmann on Stack Overflow See other posts from Stack Overflow or by Lars Tackmann
Published on 2010-04-10T23:10:14Z Indexed on 2010/04/10 23:13 UTC
Read the original article Hit count: 364

Filed under:
|
|
|

In Bash I can create a map (hashtable) with this common construction

hput() {
  eval "$1""$2"='$3'
}

hget() {
  eval echo '${'"$1$2"'#hash}'
}

and then use it like this:

hput capitols France Paris
hput capitols Spain Madrid
echo "$(hget capitols France)"

But how do I best iterate over the entries in the map ?. For instance, in Java I would do:

for (Map.Entry<String, String> entry : capitols.entrySet()) {
  System.out.println("Country " + entry.getKey() + " capital " + entry.getValue());
}

is there a common way of accomplishing something similar in Bash ?.

© Stack Overflow or respective owner

Related posts about bash

Related posts about hashtable