nullglob and arrays

不想你离开。 提交于 2019-12-08 02:01:50

问题


I can create an array, then delete from this array

$ foo=(a b c)

$ unset foo[0]

$ echo ${foo[*]}
b c

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

回答1:


unset 'foo[0]'

Bash thinks the var[1] is a glob, doesn't find a file that matches it, and per instruction of nullglob removes it, causing your script to run unset instead of unset 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



来源:https://stackoverflow.com/questions/15897473/nullglob-and-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!