linux shell global variable does not available in

2020-04-21 03:25发布

问题:

I open tow terminal windows in my linux OS. In one window, I type the command

export a=10

but in another window, I enter

echo $a

it output nothing, so why it does not work. thanks in advance.

回答1:

When you export an environmental variable, the child processes of the shell (e.g.: the commands executed by the shell) do obtain a copy of that exported variable.

Shells running in two different terminals/windows do not fulfill this relationship in terms of processes.

If you want to set an environmental variable, so that every running shell will obtain a its own instance of the variable, consider defining it in .bashrc (assuming you are using bash). However, you won't be able to use this variable as a one "being shared" by all the running shells (each shell has its own copy of the variable).



标签: linux