# Bash function dumpArray for showing content of any array # support associative array, even with UTF-8 encoded keys # (C) 2020 - 2024 - F-Hauri.ch # Licensed by GNU GENERAL PUBLIC LICENSE Version 3 # - Right align integers keys and values # - Support newlines in keys as well as in values dumpArray() { local -n _ary=$1 local _idx _align local -i _idlen=0 _idbytes _valLen case ${_ary@a} in *a*) _idlen=(${!_ary[@]}) _idlen=${#_idlen[${#_idlen[@]}-1]} ;; *A*) for _idx in "${!_ary[@]}"; do _idlen=" ${#_idx} >_idlen ? ${#_idx} : _idlen " done _align=- ;; *) return 1 ;; esac if [[ ${_ary@a} == *i* ]]; then read -r _valLen < <(printf '%d\n' "${_ary[@]}"|sort -rn) _valLen=${#_valLen} for _idx in "${!_ary[@]}"; do printf -v _ '%s%n' "$_idx" _idbytes printf "%${_align}*s: %*d\n" $((_idbytes-${#_idx}+_idlen)) "$_idx" \ $_valLen "${_ary["$_idx"]}" done else for _idx in "${!_ary[@]}"; do printf -v _ '%s%n' "$_idx" _idbytes printf "%${_align}*s: %s\n" $((_idbytes-${#_idx}+_idlen)) "$_idx" \ "${_ary["$_idx"]//$'\n'/$'\n\e['${_idlen}C }" done fi } _dumpArray() { local cur=${COMP_WORDS[COMP_CWORD]} COMPREPLY=( $(compgen -W '$( declare | sed -ne "s/^\([^ ]\+\)=(.*/\1/p" )' -- "${cur-}" ) ) } complete -F _dumpArray dumpArray