Skip to content

Instantly share code, notes, and snippets.

@sylvaindethier
Forked from coderofsalvation/is_array.bash
Last active September 6, 2017 23:50
Show Gist options
  • Save sylvaindethier/5c7669564eb815dcd9bb10fa0e89b916 to your computer and use it in GitHub Desktop.
Save sylvaindethier/5c7669564eb815dcd9bb10fa0e89b916 to your computer and use it in GitHub Desktop.
check if variable is array, returns 0 on success, 1 otherwise
## Check if variable is array
# @param mixed
# @return integer 0 on success, 1 otherwise
#
# @example
# value=("i'm an array" "and that's my 2nd value")
# if is_array value; then
# echo 'value is an Array'
# else
# echo 'value is NOT an Array'
# fi
is_array () {
# 1st arg is a null string => false (1)
[ -z "$1" ] && return 1
# bash is the runtime; $BASH string is not null
if [ -n "$BASH" ]; then
# check for array declaration on 1st arg; disable error output => true (0)
declare -p ${1} 2> /dev/null | grep 'declare \-a' >/dev/null && return 0
fi
# no result => false (1)
return 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment