How to change the value of environment variable inside shell script

10/12/2020

I have kubernetes pod environment variable

JOBID=111

I am changing this env variable from inside a shell script like below. This change is happening inside an infinite loop. so script never terminates.

export JOBID=$(echo $line)

Inside the script the value of variable is changed to new value. But if I check the value of the env varible outside the script, inisde a new terminal the value of the env variable is still 111.

-- user3553913
kubernetes
shell

1 Answer

10/12/2020

Inside the script the value of variable is changed to new value. But if I check the value of the env varible outside the script, inisde a new terminal the value of the env variable is still 111

This is how environment variables work and you cannot change it. You can only change variable for a specific process, and this env will propagate for every other process you run from this process.

But you cannot overwrite global value. Only the local value (process' copy). Every other process (e.g. started by kubectl exec) will have "old" value of the env variable.

-- Matt
Source: StackOverflow