This is an old revision of the document!
To save piles of typing when debugging bash scripts, a dump() function that understands both regular and associative arrays:
is_array() {
if [ -n "$BASH" ]; then
declare -p ${1} 2> /dev/null | grep 'declare \-a' >/dev/null && return 0
fi
return 1
}
is_hash() {
if [ -n "$BASH" ]; then
declare -p ${1} 2> /dev/null | grep 'declare \-A' >/dev/null && return 0
fi
return 1
}
dump() {
for var; do
if is_array ${var} ; then
echo ${var} = $(eval "echo \${${var}[@]}")
elif is_hash ${var} ; then
dump_hash ${var}
else
echo ${var} = $(eval "echo \$${var}")
fi
done
}
dump_hash() {
h=${1}
for k in $(eval "echo \${!${h}[@]}"); do
echo "$h[${k}]" = $(eval "echo \${${h}[${k}]}")
done
}
First, it can dump regular variables:
$ dump HOME HISTFILE HISTSIZE HOME = /home/rpjday HISTFILE = /home/rpjday/.bash_history HISTSIZE = 1000 $
Next, dumping regular arrays:
people=(fred barney wilma betty) [rpjday@localhost bash]$ dump people people = fred barney wilma betty $