LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   export creates a global environment variable? (https://www.linuxquestions.org/questions/linux-newbie-8/export-creates-a-global-environment-variable-4175659904/)

babaliaris 08-27-2019 12:11 PM

export creates a global environment variable?
 
So, until now I knew that export is creating a global environment variable that can be accessed by any process. But I tried the following and it didn't work.

I opened a terminal and typed
Code:

export a=1
Then I echoed it and got 1.

Then, I opened a new terminal and typed
Code:

echo $a
. The result is an empty line instead of 1.

Why is that? In my ~./bashrc script (provided by my Linux distribution Arch Linux) I have the following exports:
Code:

#Environment Variables
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk

#Library path
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/lib64

#PATH
export PATH=$PATH:/home/babaliaris/Programs/bin:/home/babaliaris/Programs/android-studio/bin
export PATH=$PATH:/home/babaliaris/Android/Sdk/platform-tools

and they work globally (I tested them by echoing them in different terminals).

Why this works in the bashrc script and not in the way I described at the top of this post?

Thank you.

teckk 08-27-2019 12:24 PM

~./bashrc is used every time you open bash terminal.
Put export a=1 in ~./bashrc

In one terminal enter
Code:

export PATH=fred

echo $PATH
fred

Open another terminal
Code:

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/bin:....


babaliaris 08-27-2019 12:32 PM

Quote:

Originally Posted by teckk (Post 6030211)
~./bashrc is used every time you open bash terminal.
Put export a=1 in ~./bashrc

In one terminal enter
Code:

export PATH=fred

echo $PATH
fred

Open another terminal
Code:

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/bin:....


Ohh. Now it makes sense. So what is the difference between
Code:

export a=1
and
Code:

a=1
? I think one time I tried something like this:

Code:

LIBGL_ALWAYS_SOFTWARE=1
./myapp

where myapp creates a new thread which uses the LIBGL_ALWAYS_SOFTWARE variable. But the above example didn't work, while the one below worked:

Code:

export LIBGL_ALWAYS_SOFTWARE=1
./myapp


ondoho 08-27-2019 12:45 PM

Quote:

Originally Posted by babaliaris (Post 6030201)
So, until now I knew that export is creating a global environment variable that can be accessed by any process.

It does this only for the currently running shell. Which ends when you close your terminal, and is not connected to another shell opened in another terminal.

There is, however, something called a login shell, which parses various startup files (e.g. ~/.bash_profile if you're using bash). It's some kind of Uber-shell. I'm sure somebody will be along shortly and give you the technical details. Or you could perform a web search.

ehartman 08-27-2019 01:15 PM

Quote:

Originally Posted by babaliaris (Post 6030215)
Ohh. Now it makes sense. So what is the difference between
Code:

export a=1
and
Code:

a=1
?

The latter is a pure local variable for that shell only, the first will be inherited by all programs that are started after that export has been done (so all children, run in that specific shell).
BTW: you can export a variable even before setting (or changing) it, but afterwards too.
So both
Code:

export a
<some other commands>
a=1

as well as
Code:

a=1
<several other commands>
export a

are legal. Sometimes you do not even have to export at all, like i.e. PATH, TERM, they have already been exported by the shell, but you can locally change their value.

Once exported, the variable stays in the enviroment.

rnturn 08-27-2019 01:33 PM

Quote:

Originally Posted by babaliaris (Post 6030201)
So, until now I knew that export is creating a global environment variable that can be accessed by any process. But I tried the following and it didn't work.

I opened a terminal and typed
Code:

export a=1
Then I echoed it and got 1.

Then, I opened a new terminal and typed
Code:

echo $a
. The result is an empty line instead of 1.

When you opened the new terminal, you created a fresh new environment based on the one created when you logged in and your GUI was started. Create another terminal and you created another fresh environment. Without doing anything, those two environments will be the same. But once you start creating new variables, exporting them, etc. in one terminal, those environments begin to differ and changes in one are not reflected in the other.

Does that make sense?

It'd sure be nice if I could make a change that I'd like to be "global" after I've already opened terminals and applications in ten different activity windows without havng to log out and back in again but UNIX/Linux doesn't work that way. All you can do is plan ahead: make the change in whatever defines your environment at login and it'll be there when you login the next time in all the terminals you open up.

HTH...

babaliaris 08-27-2019 02:05 PM

Everything makes sense now guys. One last thing. The first program that starts inside a terminal, for example:
Code:

export myvar = 5
./myapp

it is also a child of the bash's process right? What I want to say is that it starts a new process instead of living inside the bash's process as a thread (probably this is a stupid question, of course, it starts a new process). I need to export myvar so that myapp can read it, right?

ehartman 08-27-2019 03:01 PM

Quote:

Originally Posted by babaliaris (Post 6030269)
it is also a child of the bash's process right? What I want to say is that it starts a new process instead of living inside the bash's process as a thread

Any "real" executable (binary) will be a new process.
A shell script can be run either by a new (child) instance of the shell (or a different one, if you used #!/bin/<someshell> as its first line) OR by the current shell itself. In that case the alternate shell line is ignored, and you have to start it as
Code:

. script    or
source script

The latter posibility makes it possible to alter the environment OF that shell itself and is the way .bashrc and .bash_profile startup scripts are run.

scasey 08-27-2019 03:55 PM

Quote:

Originally Posted by rnturn (Post 6030252)
It'd sure be nice if I could make a change that I'd like to be "global" after I've already opened terminals and applications in ten different activity windows without havng to log out and back in again but UNIX/Linux doesn't work that way. All you can do is plan ahead: make the change in whatever defines your environment at login and it'll be there when you login the next time in all the terminals you open up.

True.
A workaround is to make such a change in "whatever defines your environment at login" (e.g. ~/.bashrc) in one terminal and then source it:
Code:

. ~/.bashrc
in other, already opened, terminals to effect the change therein without logging out.

ondoho 08-28-2019 01:57 PM

Quote:

Originally Posted by babaliaris (Post 6030269)
I need to export myvar so that myapp can read it, right?

yes.


All times are GMT -5. The time now is 10:25 AM.