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

I am using 4.2.53 (1) -release, which runs Fedora 20.

The following two parts of the code behave differently, can anyone explain why? Thanks.

[hidden]$ unset x; declare -px; function f() { declare -A -gx; x[10]=100; }; f; declare -px; -bash: declare: x: not found declare -A x='([10]="100" )' [hidden]$ unset x; declare -px; function f() { declare -A -gx=(); x[10]=100; }; f; declare -px; -bash: declare: x: not found declare -A x='()' 
+5
source share
1 answer

This was a bug in 4.0-4.2. This was 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. 

Here is the result at 4.3, where they behave identically:

 $ echo $BASH_VERSION 4.3.11(1)-release $ unset x; declare -px; function f() { declare -A -gx; x[10]=100; }; f; declare -px; bash: declare: x: not found declare -A x='([10]="100" )' $ unset x; declare -px; function f() { declare -A -gx=(); x[10]=100; }; f; declare -px; bash: declare: x: not found declare -A x='([10]="100" )' 
+5
source

Source: https://habr.com/ru/post/1208278/


All Articles