I am trying to see if an array is empty in bash
key=[]
key1=["2014"]
I have tried following ways:
[[ -z "$key" ]] && echo "empty" || echo "not Empty"
[[ -z "$key1" ]] && echo "empty" || echo "not Empty"
Both return 'not Empty'
[[ $key==[] ]] && echo "empty" || echo "not Empty"
[[ $key1==[] ]] && echo "empty" || echo "not Empty"
Both return empty
.
key=[]
isn't an array; it's a regular variable with the value[]
. Bash doesn't really have empty arrays: it has unset variables (which may or may not have the array attribute set), and it has array variables with one or more values assigned to them. – Unhandsome