red888
red888

Reputation: 31570

iterate over a map and export vars

this script prints out the right values but it looks like my export statement isn't correct:

declare -A ENVMAP=( [foo]=bar [baz]=quux [corge]=grault )
for i in "${!ENVMAP[@]}"
do
  echo "key  : $i"
  echo "value: ${ENVMAP[$i]}"
  export "$i"="${ENVMAP[$i]}"
done

The value gets printed correctly so the map works but the vars aren't getting exported. Whats wrong with my export statement?

Upvotes: 2

Views: 64

Answers (2)

RavinderSingh13
RavinderSingh13

Reputation: 133538

In case you want to have these vars permanently in your user's system you could add them into your current user's DOT/BASH profile too, few steps are as follows:

I- Go to current user's directory by doing normal cd.

II- Then list all files including hidden ones by doing either ls -la or ls -lhtr.

III- Now either add these export commands in your BASH profile(in case you are on Linux/bash) or you could create your own .profile too(only one will be picked up at a time).

IV- Once you saved it then use source .profile or source .bash_profile or . ./.profile or . /.bash_profile.

Please try these and let me know if these helps you.

Upvotes: 1

LMC
LMC

Reputation: 12702

How are you running the script? the exported variables are visible to the child processes of your script but not to the parent, i.e. your bash console.

Upvotes: 1

Related Questions