I found an awesome answer on StackOverflow which explains how to pass an associative array to a function. Would someone be able to help me figuring out what the syntax of ${1#*=}
in the code below specifies? (Borrowed from that answer by jaypal singh):
#!/bin/bash
declare -A weapons=(
['Straight Sword']=75
['Tainted Dagger']=54
['Imperial Sword']=90
['Edged Shuriken']=25
)
function print_array {
eval "declare -A arg_array="${1#*=}
for i in "${!arg_array[@]}"; do
printf "%s\t%s\n" "$i ==> ${arg_array[$i]}"
done
}
print_array "$(declare -p weapons)"
Here's my guess so far (correct me if I'm wrong on any of these):
- 1
means the first parameter passed to the function ($1
or ${1}
)
- #
means the index of $1
, which, since $1
is an associative array, makes #
the keys of $1
- *
means the values of of the #
keys in associate array $1
That leaves the =
. What does that signify? Is that like a way to show that you want #
and *
to mean the keys and values of the associate array?