3 SC2184
malamut-ubuntu edited this page 2022-02-01 22:06:51 +03:00

Quote arguments to unset so they're not glob expanded.

Problematic code:

unset foo[index]

Correct code:

unset 'foo[index]'

Rationale:

Arguments to unset are subject to regular glob expansion. This is especially relevant when unsetting indices in arrays, where [..] is considered a glob character group.

In the problematic code, having a file called food in the current directory will result in unset foo[index] expanding to unset food, which will silently succeed without unsetting the element.

Quoting so that the [..] is passed literally to unset solves the issue.

Note that you can unset element using variable for index name like this:

unset 'foo[$var]'

Exceptions:

If you know that pathname expansion is disabled you can ignore this message. set -o noglob (and variations like invoking the script with #!/bin/bash -f) will prevent glob expansion of arguments to unset.