I have a loop over variable names and I need to check if content of a variable is an array or not:
for varname in AA BB CC; do local val if [ "$varname" is array ]; then # how can I perform this test? echo do something with an array else echo do something with a "'normal'" variable fi done
How do I do it?
An array is a variable containing multiple values. Any variable may be used as an array. There is no maximum limit to the size of an array, nor any requirement that member variables be indexed or assigned contiguously.
To check if an array contains an element or not in Python, use the in operator. The in operator checks whether a specified element is an integral element of a sequence like string, array, list, tuple, etc.
To avoid a call to grep, you could use:
if [[ "$(declare -p variable_name)" =~ "declare -a" ]]; then echo array else echo no array fi
According to this wiki page, you can use this command:
declare -p variable-name 2> /dev/null | grep -q '^declare \-a'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With