arrays exported in functions are not exported
This came as a bit of a shock, but having a function export a variable declared as an array doesn't actually export that variable. And if the variable already exists, it doesn't change the value. It seems that passing by name is the only way to do that, but I have to experiment some more.
For the record, this does not work:
T282828
Te6edf3set Tff7b72-Te6edf3eux
Te6edf3fooTb4b4b4(Tb4b4b4)Tb4b4b4{
Te6edf3declare Tff7b72-Te6edf3a Te6edf3FOOTff7b72=Tb4b4b4(Te6edf3a Te6edf3b Te6edf3cTb4b4b4)
Tff7b72export Te6edf3FOO
Tb4b4b4}
Te6edf3echo Ta5d6ff"Ta5d6ff${#FOO}Ta5d6ff"
Nor does setting the already existing array to a new value inside a function.
This works, provided the version of Bash is new enough (note, not CentOS 7.x):
T282828
set -eux
declare -a FOO=(c d e f g)
foo(){
local -n ARR="Tff7b72${T79c0ff1Tff7b72}"
ARR=(a b c)
}
foo FOO
echo "Tff7b72${T8b949e#FOO[@]Tff7b72}" #note it will be 3 per foo()
Tags: bash
Tags
Navigation