Array - Export/Import in global environment variables.

Hello.
During startup /etc/bash.bashrc.local generates some array

.....
source /.../.../system_common_general_array_env_var
.....

The file system_common_general_array_env_var contains :

LEAP_VERSION='42.3'
ARRAY_MAIN_REPO_LEAP=('zypper_local' 'openSUSE-Leap-'"$LEAP_VERSION"'-Non-Oss'  'openSUSE-Leap-'"$LEAP_VERSION"'-Oss'  'openSUSE-Leap-'"$LEAP_VERSION"'-Update'  'openSUSE-Leap-'"$LEAP_VERSION"'-Update-Non-Oss')
declare -p ARRAY_MAIN_REPO_LEAP > /tmp/ARRAY_MAIN_REPO_LEAP.txt

During loggon user's .bashrc run a bash function rebuild_shared_array defined as this

.....
#
# ++++++++++++++++++++++++++++++++++++++++++++
#
# rebuild_shared_array
#
# ++++++++++++++++++++++++++++++++++++++++++++
#
function rebuild_shared_array() {
set -x
.....
    source /tmp/ARRAY_MAIN_REPO_LEAP.txt
.....
set +x

}

.....

the file /tmp/ARRAY_MAIN_REPO_LEAP.txt contains

declare -a ARRAY_MAIN_REPO_LEAP='([0]="zypper_local" [1]="openSUSE-Leap-42.3-Non-Oss" [2]="openSUSE-Leap-42.3-Oss" [3]="openSUSE-Leap-42.3-Update" [4]="openSUSE-Leap-42.3-Update-Non-Oss")'

In the session, when I open a terminal, due to the instruction 'set -x' I can see that array files has been sourced.
But when I type

echo ${ARRAY_MAIN_REPO_LEAP[@]}

I got nothing.
I have to type manually

source /tmp/ARRAY_MAIN_REPO_LEAP.txt

Why calling the function 'rebuild_shared_array' in .bashrc does not do the job.
So how to do it.

Any help is welcome.

Hi,
You are this problem because when used in a function, the built-in declare makes local variable. The -g option suppresses this behavior.

A solution could be:

declare -p ARRAY_MAIN_REPO_LEAP | awk '$2="-g "$2' > /tmp/ARRAY_MAIN_REPO_LEAP.txt

Regards.

1 Like

Great.