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

Kan*_* Li 5 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='()'
Run Code Online (Sandbox Code Playgroud)

tha*_*guy 5

这是4.0-4.2中的一个错误.它在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.
Run Code Online (Sandbox Code Playgroud)

这是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" )'
Run Code Online (Sandbox Code Playgroud)