In Bash, given an associative array, how do I find the length of the longest key?
Say, I declare myArray
as shown below:
$ declare -A myArray=([zero]=nothing [one]='just one' [multiple]='many many')
$ echo ${myArray[zero]}
nothing
$ echo ${myArray[one]}
just one
$ echo ${myArray[multiple]}
many many
$
I can get it using the below one-liner
$ vSpacePad=`for keys in "${!myArray[@]}"; do echo $keys; done | awk '{print length, $0}' | sort -nr | head -1 | awk '{print $1}'`;
$ echo $vSpacePad
8
$
Am looking for something simpler like below, but unfortunately, these just give the count of items in the array.
$ echo "${#myArray[@]}"
3
$ echo "${#myArray[*]}"
3