1

I want to store environment variables in a bash array, and then use them in cases where I perform the same operation on all of them (unset, export an so on). For example, if I want to print all env vars in my array:

envVar1="firstVar"
envVar2="secondVar"
envVar3="thirdVar"
envVars=(envVar1 envVar2 envVar3)
for currentEnvVar in "${envVars[@]}" 
do
    # print env var name and value
    printf "Initial Value: %s = %s\n" "${currentEnvVar}" "${currentEnvVar}"
    # unset env var
    printf "Unsetting %s\n" "${currentEnvVar}"
    unset "${currentEnvVar}"

    # print env var name and value
    printf "Unset Variable (nothing should be to right of '=') %s = %s\n\n" "${currentEnvVar}" "${currentEnvVar}"

    # set env var 
    printf "Setting %s\n" "${currentEnvVar}"
    export "${currentEnvVar}"="${currentEnvVar}""_new"

    # print env var name and new value...
    printf "New Value %s =  %s\n\n" "${currentEnvVar}" "${currentEnvVar}"

done

What I am unsure of is how to store the variables in envVars and how to use them in the loop.

My goal is for the output of the above loop to be:

Initial Value: envVar1 = firstVar
Unsetting envVar1
Unset Variable (nothing should be to right of '=') envVar1 =

Setting envVar1
New Value envVar1 =  firstVar_new

Initial Value: envVar2 = secondVar
Unsetting envVar2
Unset Variable (nothing should be to right of '=') envVar2 =

Setting envVar2
New Value envVar2 =  secondVar_new

Initial Value: envVar3 = thirdVar
Unsetting envVar3
Unset Variable (nothing should be to right of '=') envVar3 =

Setting envVar3
New Value envVar3 = thirdVar_new

But what I get is this:

Initial Value: envVar1 = envVar1
Unsetting envVar1
Unset Variable (nothing should be to right of '=') envVar1 = envVar1

Setting envVar1
New Value envVar1 =  envVar1

Initial Value: envVar2 = envVar2
Unsetting envVar2
Unset Variable (nothing should be to right of '=') envVar2 = envVar2

Setting envVar2
New Value envVar2 =  envVar2

Initial Value: envVar3 = envVar3
Unsetting envVar3
Unset Variable (nothing should be to right of '=') envVar3 = envVar3

Setting envVar3
New Value envVar3 =  envVar3

Is this even possible? How do I set up the array of environment variable names then use, or modify, their values?

1 Answer 1

2

There are a couple of different pieces to what you seem to be trying to do.

To unset a variable whose name is in another variable, you can use -v so

unset -v "$currentEnvVar"

for that part. See here

To assign to a variable you can use printf like

printf -v "$currentEnvVar" %s 'new value'

To print the value you can use an indirect reference like

printf 'The value is %s\n' "${!currentEnvVar}"

In brace expansion if the first character is ! the rest is treated as the name of a variable that contains the name of the variable you want to actually use.

You could also consider using an associative array instead, if it suits your needs:

declare -A envVarsByName
envVarsByName[$currentEnvVar]='new value'
printf 'New val: %s\n' "${envVarsByName[$currentEnvVar]]}"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .