3

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
0

1 Answer 1

4
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

2
  • I spent two days and finally found that nullglob was the culprit! Thanks for the correct way to use unset Jun 2, 2015 at 0:24
  • With a variable as the subscript, use double quotes, like this: `unset -v "var[$subscript]". Oct 20, 2018 at 19:19

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.