In zsh
I can easily dump the contents of an associative array with a single command:
zsh% typeset -A foo
zsh% foo=(a 1 b 2)
zsh% typeset foo
foo=(a 1 b 2 )
However despite searching high and low, the best I could find was declare -p
, whose output contains declare -A
:
bash$ typeset -A foo
bash$ foo=([a]=1 [b]=2)
bash$ declare -p foo
declare -A foo='([a]="1" [b]="2" )'
Is there a clean way to obtain something like the zsh
output (ideally foo=(a 1 b 2 )
or foo='([a]="1" [b]="2" )'
), preferably without resorting to string manipulation?
It seems there is no way to do this other than string manipulation. But at least we can avoid forking a sed
process each time, e.g.:
dump_assoc_arrays () {
for var in "$@"; do
read debug < <(declare -p $var)
echo "${debug#declare -A }"
done
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With