'declare -A x'vs'declare -A x =()'

时间:2014-12-03 18:32:26

标签: linux bash shell

我正在使用4.2.53(1)-release,它由Fedora 20运行。

以下两段代码的行为有所不同,任何人都能说出原因吗?感谢。

[hidden]$ unset x; declare -p x; function f() { declare -A -g x; x[10]=100; }; f; declare -p x;
-bash: declare: x: not found
declare -A x='([10]="100" )'
[hidden]$ unset x; declare -p x; function f() { declare -A -g x=(); x[10]=100; }; f; declare -p x;
-bash: declare: x: not found
declare -A x='()'

1 个答案:

答案 0 :(得分:5)

这是4.0-4.2中的错误。那是fixed in 4.3

ddd. Fixed several bugs that caused `declare -g' to not set the right global
     variables or to misbehave when declaring global indexed arrays.

这是4.3的结果,它们的行为相同:

$ echo $BASH_VERSION
4.3.11(1)-release

$ unset x; declare -p x; function f() { declare -A -g x; x[10]=100; }; f; declare -p x;
bash: declare: x: not found
declare -A x='([10]="100" )'

$  unset x; declare -p x; function f() { declare -A -g x=(); x[10]=100; }; f; declare -p x;
bash: declare: x: not found
declare -A x='([10]="100" )'