In Bash test if associative array is declared
I want to test a variable is declared as an associative array or not. I see the above discussion. But it is not clear which one is the best.
The following mentioned on the above link, but does not work.
declare -A x; [[ -v x[@] ]]; echo "$?"
This is costly. It has to print the whole data structure. It can be slow when the data is large.
[[ "$(declare -p FOO 2>/dev/null)" == "declare -A"* ]]
What is the best way to check whether a variable is an associative array?
EDIT: The answer below is the best for Bash 5. The results on the link at the top of the message are obsolete and those methods should be ignored for Bash 5.