[英]Why the `nounset` shell option doesn't raise an error on unset arrays?
Why no errors are raised when unset
is set, and an undefined array is referenced?为什么在unset
并且引用了未定义的数组时不会引发错误?
Am i missing something, eg this is controlled by another option?我是否遗漏了什么,例如这是由另一个选项控制的?
Example:例子:
set -o nounset
echo "${foo[*]}" # no error
echo "${foo[@]}" # no error
echo "$foo" # error
I've checked the manpage , but I'm puzzled, because it mentions $@
and $*
, but not arrays:我检查了 manpage ,但我很困惑,因为它提到了$@
和$*
,但没有提到数组:
Treat unset variables and parameters other than the special parameters '@' or '*' as an error将未设置的变量和特殊参数 '@' 或 '*' 以外的参数视为错误
The Bash version I'm using is 5.0.我使用的 Bash 版本是 5.0。
I believe this is just a shortcoming of the documentation;我相信这只是文档的一个缺点; it should mention array parameters index with @
and *
as well.它还应该使用@
和*
提及数组参数索引。
The intent is to make it look like an array can be "set" to an empty value.目的是让它看起来像一个可以“设置”为空值的数组。 However,然而,
foo=()
doesn't actually assign an empty array value to the name foo
.实际上并没有为名称foo
分配一个空数组值。 It simply sets the array attribute on the name foo
, and clears any values that may already have been present.它只是在名称foo
上设置数组属性,并清除可能已经存在的任何值。 $foo
and ${foo[0]}
are equivalent, and both will be treated as an unset parameter. $foo
和${foo[0]}
是等价的,两者都将被视为未设置的参数。 Each of ${foo[@]}
and ${foo[@]}
are special cased to be "set", but "empty", as far as -u
is concerned.就-u
而言, ${foo[@]}
和${foo[@]}
都被特例为“设置”,但为“空”。
Note, though, that the array attribute doesn't actually need to be set for the @
/ *
indices to override -u
.但请注意,实际上并不需要为@
/ *
索引设置数组属性来覆盖-u
。
$ unset bar
$ set -o nounset
$ echo ${bar[@]}
$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.