-
-
Save sylvaindethier/5c7669564eb815dcd9bb10fa0e89b916 to your computer and use it in GitHub Desktop.
check if variable is array, returns 0 on success, 1 otherwise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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