LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-27-2019, 01:11 PM   #1
babaliaris
LQ Newbie
 
Registered: Jul 2018
Location: Greece
Distribution: Arch Linux
Posts: 28

Rep: Reputation: Disabled
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.

Last edited by babaliaris; 08-27-2019 at 01:13 PM.
 
Old 08-27-2019, 01:24 PM   #2
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 2,519

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
~./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:....
 
1 members found this post helpful.
Old 08-27-2019, 01:32 PM   #3
babaliaris
LQ Newbie
 
Registered: Jul 2018
Location: Greece
Distribution: Arch Linux
Posts: 28

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by teckk View Post
~./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
 
Old 08-27-2019, 01:45 PM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 13,147
Blog Entries: 9

Rep: Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603
Quote:
Originally Posted by babaliaris View Post
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.
 
1 members found this post helpful.
Old 08-27-2019, 02:15 PM   #5
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,111

Rep: Reputation: 632Reputation: 632Reputation: 632Reputation: 632Reputation: 632Reputation: 632
Quote:
Originally Posted by babaliaris View Post
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.

Last edited by ehartman; 08-27-2019 at 02:16 PM.
 
Old 08-27-2019, 02:33 PM   #6
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: Currently: openSUSE, Raspbian, Slackware. Formerly: CentOS, MacOS, Red Hat. Other: Solaris, Tru64
Posts: 1,670

Rep: Reputation: 202Reputation: 202Reputation: 202
Quote:
Originally Posted by babaliaris View Post
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...
 
1 members found this post helpful.
Old 08-27-2019, 03:05 PM   #7
babaliaris
LQ Newbie
 
Registered: Jul 2018
Location: Greece
Distribution: Arch Linux
Posts: 28

Original Poster
Rep: Reputation: Disabled
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?

Last edited by babaliaris; 08-27-2019 at 03:17 PM.
 
Old 08-27-2019, 04:01 PM   #8
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,111

Rep: Reputation: 632Reputation: 632Reputation: 632Reputation: 632Reputation: 632Reputation: 632
Quote:
Originally Posted by babaliaris View Post
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.
 
1 members found this post helpful.
Old 08-27-2019, 04:55 PM   #9
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.7.1908
Posts: 4,250

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
Quote:
Originally Posted by rnturn View Post
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.
 
1 members found this post helpful.
Old 08-28-2019, 02:57 PM   #10
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 13,147
Blog Entries: 9

Rep: Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603
Quote:
Originally Posted by babaliaris View Post
I need to export myvar so that myapp can read it, right?
yes.
 
1 members found this post helpful.
  


Reply

Tags
environment variables, export, linux


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Export proxy variable not the same as gnome network manager global system proxy!?!???? gnoma Linux - Networking 2 11-17-2017 08:20 AM
In c using local variable as seperate functions or a global variable for a const Alpha90 Programming 4 06-15-2015 07:04 AM
How to set a global environment variable? thomas2004ch Linux - Software 2 01-11-2013 07:34 AM
Global (shell) environment variable question Seventh Linux - Newbie 1 04-05-2007 04:18 PM
LXer: Piracy creates jobs, FOSS creates opportunities LXer Syndicated Linux News 0 11-02-2006 12:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration