Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

bash

shell

I am using 4.2.53(1)-release, which is run by the Fedora 20.

The following two pieces of code behave differently, can anyone tell why? Thanks.

[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='()'
like image 916
Kan Li Avatar asked Dec 03 '14 18:12

Kan Li


1 Answers

This was a bug in 4.0-4.2. It 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's the result on 4.3, where they behave identically:

$ 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" )'
like image 190
that other guy Avatar answered Sep 26 '22 03:09

that other guy