gpt4 book ai didi

arrays - nullglob 和数组

转载 作者:行者123 更新时间:2023-11-29 09:48:38 26 4
gpt4 key购买 nike

我可以创建一个数组,然后从这个数组中删除

$ 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 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

关于arrays - nullglob 和数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15897473/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com