Quantcast
Channel: How do I check if variable is an array? - Stack Overflow
Browsing all 10 articles
Browse latest View live

Answer by Fonic for How do I check if variable is an array?

I usually use the following functions to check for arrays/dicts (e.g. to verify a sourced configuration):# Check if variable is an array [$1: variable name]function is_array() { [[ "$(declare -p --...

View Article



Answer by Mingye Wang for How do I check if variable is an array?

miken32's improvement actually causes a regression. The followinga=()export areadonly adeclare -p aprintsdeclare -arx a=()which would match if you were just checking for declare -a as a substring, but...

View Article

Answer by Sam Liddicott for How do I check if variable is an array?

I think this does it, it checks that the array indexes don't match "" or "0"It should be cheap for small arrays and non-arrays.It works by testing for any non-zero character in the array...

View Article

Answer by Josef Ababat for How do I check if variable is an array?

Another Way:Example, create an Array:Variable=(The Quick Brown Fox...)Test the Variable:if [ "${#Variable[@]}" -gt "1" ] ; then echo "Variable is an Array"; else echo "Variable is NOT an Array" ; fi

View Article

Answer by miken32 for How do I check if variable is an array?

I started with Reuben's great answer above. I implemented a few of the comments and some of my own improvements and came out with this:#!/bin/basharray_test() { # no argument passed [[ $# -ne 1 ]]...

View Article


Answer by Marco for How do I check if variable is an array?

Since bash 4.3 it is not that easy anymore.With "declare -n" you can add a reference to another variable and you can do this over and over again. As if this was not complicated enough, with "declare...

View Article

Answer by dawesh for How do I check if variable is an array?

is_array() { local variable_name=$1 [[ "$(declare -p $variable_name)" =~ "declare -a" ]]}is_array BASH_VERSINFO && echo BASH_VERSINFO is an arrayis_array() { local variable_name=$1 [[...

View Article

Answer by Reuben W for How do I check if variable is an array?

To avoid a call to grep, you could use:if [[ "$(declare -p variable_name)" =~ "declare -a" ]]; then echo arrayelse echo no arrayfi

View Article


Answer by Bastien Jansen for How do I check if variable is an array?

According to this wiki page, you can use this command:declare -p variable-name 2> /dev/null | grep -q '^declare \-a'

View Article


How do I check if variable is an array?

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?...

View Article
Browsing all 10 articles
Browse latest View live




Latest Images