我可以创建一个数组,然后从这个数组中删除
$ foo=(a b c)
$ unset foo[0]
$ echo ${foo[*]}
b c
但是,如果设置了 nullglob
,那么我无法从数组中删除
$ shopt -s nullglob
$ foo=(a b c)
$ unset foo[0]
$ echo ${foo[*]}
a b c
最佳答案
unset 'foo[0]'
Bash thinks the
var[1]
is a glob, doesn't find a file that matches it, and per instruction ofnullglob
removes it, causing your script to rununset
instead ofunset var[1]
- and nothing gets unset. The correct way to fix this issue is to quote the variable name (and always specify-v
explicitly):unset -v 'var[1]'
.
关于arrays - nullglob 和数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15897473/