2014-12-03 153 views
5

我使用4.2.53(1) - 釋放,這是由Fedora 20.'聲明-A X' VS '聲明-A X =()'

下面的兩個代碼的行爲的運行件不同的是,有誰能說出原因嗎謝謝。

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

回答

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