On Fri, Aug 29, 2014 at 05:19:03PM +0100, Jason Vas Dias wrote: > Aha! Yes, that was it . Thank you! > I wouldn't have expected expansion to occur for a name in single quotes. > > I still think it would be nice to have the ability to tell if an array has > been > declared associative or not. What about a 'test -A my_array' operator ?
$ unset a b; declare -A a; a[1]=foo; b=(an array); declare -p a b declare -A a='([1]="foo" )' declare -a b='([0]="an" [1]="array")' If you want to mangle that into a test, it would be something like: if [[ $(declare -p a) = declare\ -A* ]]; then I still don't understand what your function is trying to DO, though. Whatever it is, it looks extremely awkward and painful. My advice to anyone who is trying to pass the name of an array as an argument to a function in Bash is usually "Stop! Use a real programming language instead." Even declare -n didn't give us the ability to do this safely. If you insist on doing this, you need to check all the user input before handing it to eval.