nullglob和数组 [英] nullglob and arrays
本文介绍了nullglob和数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以创建一个数组,然后从该数组中删除
I can create an array, then delete from this array
$ foo=(a b c)
$ unset foo[0]
$ echo ${foo[*]}
b c
但是,如果设置了nullglob
,则无法从阵列中删除
However if nullglob
is set then I cannot delete from the array
$ shopt -s nullglob
$ foo=(a b c)
$ unset foo[0]
$ echo ${foo[*]}
a b c
推荐答案
unset 'foo[0]'
Bash认为
var[1]
是一个问题,找不到与之匹配的文件,并且nullglob
的每条指令将其删除,从而导致脚本运行unset
而不是unset var[1]
-并没有任何设置.解决此问题的正确方法 问题是引用变量名(并始终显式指定-v
):unset -v 'var[1]'
.
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]'
.
这篇关于nullglob和数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文