我正在尝试在Bash中检查一个数组是否为空
key=[]
key1=["2014"]
我尝试了以下方法:
[[ -z "$key" ]] && echo "empty" || echo "not Empty"
[[ -z "$key1" ]] && echo "empty" || echo "not Empty"
两个都返回“not Empty”
[[ $key==[] ]] && echo "empty" || echo "not Empty"
[[ $key1==[] ]] && echo "empty" || echo "not Empty"
两者都返回“empty”。
key=[]
不是一个数组;它是一个普通的变量,其值为[]
。Bash实际上没有空数组:它有未设置的变量(可能具有数组属性),以及已分配一个或多个值的数组变量。 - chepner