Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nullglob and arrays

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
like image 443
Zombo Avatar asked Oct 27 '25 02:10

Zombo


1 Answers

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

like image 183
Zombo Avatar answered Oct 29 '25 18:10

Zombo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!