I am attempting to check if an argument is an array with the following code:
if [[ $(declare -p $1) ]] != *-a*;
Here $1 is a string with the value "123". I get the following error message from bash:
`arrays.bash: line 23: declare: 123: not found
This code works if I pass an array as an argument but not a string. I want to verify that the argument is either an array or an associative array. I have no concern with the contents at this point, I only want the type. Any ideas on how to do this?
$1is always a string. Always. It cannot be anything else. That also means it cannot be an array.$1, but it's still a string.$1refers to, with the assumption that the string is in fact a variable name; it's falling when you aren't passing a variable name at all. This is to be expected.yourfunc coryourfunc "$c"oryourfunc "${c[@]}"or something else. Runnable code is far more precise than English-language descriptions of code.