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

以下两段代码的行为有所不同,谁能说出原因?谢谢。

[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='()'

最佳答案

这是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" )'

关于linux - 'declare -A x'vs 'declare -A x=()',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27279087/

10-12 02:15