Shell – the use of “export” command

environment-variablesshell

I created a environment variable in one terminal window and tried to echo it in another terminal window. That displayed nothing.

$TEST=hello

After that I exported it and tried again to echo it in a different terminal window. result was same as before.

export TEST 

but if I execute the same code at the login (appending the code to ~/.profile file) variables can be used any terminal window. What is happening here? What is the different between executing a code in a terminal and executing the same at the login?

Best Answer

export makes a variable something that will be included in child process environments. It does not affect other already existing environments. In general there isn't a way to set a variable in one terminal and have it automatically appear in another terminal, the environment is established for each process on its own.

Adding it to your .profile makes it so that your environment will be setup to include that new variable each time you log in though. So it's not being exported from one shell to another, but instead is instructing a new shell to include it when it sets up the initial environment.

Related Question