The test case is as follows:
sourced-file.sh:
#!/bin/bash
declare -A MY_MAP=();
function C_function() {
MY_MAP[foo]="bar";
}
- First test
test.sh
#!/bin/bash
function A_function() {
source sourced-file.sh
B_function;
declare -p MY_MAP;
}
function B_function() {
C_function;
declare -p MY_MAP;
}
A_function;
Running ./test.sh
prints:
declare -A MY_MAP=([foo]="bar" )
declare -A MY_MAP=([foo]="bar" )
- Second test
test.sh
#!/bin/bash
function A_function() {
source sourced-file.sh
B_function;
declare -p MY_MAP;
}
function B_function() {
source sourced-file.sh
C_function;
declare -p MY_MAP;
}
A_function;
Running ./test.sh
now prints:
declare -A MY_MAP=([foo]="bar" )
declare -A MY_MAP=()
- third test
test.sh
#!/bin/bash
function A_function() {
B_function;
declare -p MY_MAP;
}
function B_function() {
source sourced-file.sh
C_function;
declare -p MY_MAP;
}
A_function;
Running ./test.sh
now prints:
declare -A MY_MAP=([foo]="bar" )
./test.sh: line 5: declare: MY_MAP: not found
I'm using bash 4.4.23
, and I'd like to understand this behavior. Could someone shed light on this?