bash v3
function key_val () {
case $1 in
"foo") echo "bar";;
"baz") echo "qux";;
*) echo "default";;
esac
}
for key in "foo" "baz"; do
echo "$key: $(key_val $key)"
done
bash v4
declare -A arr
arr=([foo]=bar [baz]=qux)
for key in ${!arr[@]}; do
echo "$key: ${arr[$key]}"
done
declare -A arr
arr=([foo]=bar [baz]=qux)
for key value in ${(kv)arr}; do
echo "$key: $value"
done
#!/bin/bash
arr=(
"key value"
"foo bar"
)
for item in "${arr[@]}"; do
key=$(echo $item | cut -d " " -f 1)
value=$(echo $item | cut -d " " -f 2)
echo "$key: $value"
done