Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 03-08-2012, 05:15 AM   #1
LQ Newbie
Registered: Jan 2012
Location: London, UK
Posts: 9

Rep: Reputation: Disabled
Why do I only see environment variable defined when using bash console


On the linux box I log into, bash is not running after login. I usually run bash and then proceed. I have set an environment variable using export to be persistent when in bash. But if I do eg echo $MY_VAR before I start bash, then the env variable isn't displayed. But it is after starting bash.

I assumed that it didn't matter in what shell you set the env var. Is this incorrect?

So env variables are specific to particular shells?

Old 03-08-2012, 07:17 AM   #2
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
Yes, the environment is specific to the shell. If you use export you can make the variable visible to subsequent commands (ie to the shells created from the current shell) - see the export command description in man bash.

If you want a variable available to all your user shells you could set it in ~/.bashrc - if you want it available to all users' and all system shells there is another general profile you can set (but I can't recall it).

There is no way afaik that you can set an env variable and have it visible to any shells already running.
Old 03-08-2012, 07:18 AM   #3
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,577
Blog Entries: 31

Rep: Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196
What is run when you log in? It is the last field of your user's entry in /etc/passwd. If it is not bash you could change it. Environment variables are conveniently set up for bash by Bash Startup Files.
Old 03-08-2012, 08:32 AM   #4
Registered: Oct 2010
Posts: 606

Rep: Reputation: 59
Go in your home directory and then issue this command:

PHP Code:
 ls -ld .* 
Do you see any of these files?

PHP Code:
-rw-r--r--  1 lsadm lsadm   176 Oct 21  2008 .bash_profile
-rw-r--r--  1 lsadm lsadm   124 Oct 21  2008 .bashrc 
or ".profile" or ".sh_profile"?

Edit any of them and set and export your variables in ".bash_profile", for example.

Next time you will log in, you will get the variable's value.

To make the changes effective without logging out and logging again, do this:

PHP Code:
source .bash_profile 

Last edited by devUnix; 03-08-2012 at 08:33 AM.
Old 03-10-2012, 09:07 AM   #5
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,842

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
It's really incorrect to say that you make an environment setting "permanent". All processes only have values that are set internally to that process (AFAIK).

What generally happens is that the environment of a new process is initialized with copies of any exported values from the parent process, and/or whatever it loads through any start-up routines (e.g. bashrc and similar files for shells).

After initialization values can only be added or altered from within the process itself (although there are techniques, such as sourcing files, that can make a process import a value from an external source). In particular values can never move "up" into parent processes, which would be a terrible security hole. And of course, whenever a process is terminated, its environment is lost too.

As for the OP's question, bashrc is usually loaded only by non-login interactive shells. You'll have to set the value in another file such as /etc/profile or ~/.bash_profile. See the INVOCATION section of your bash man page for an explanation of which files your distro's version loads in each case.


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
Setting An Environment Variable In Bash? carlosinfl Red Hat 7 02-24-2012 06:55 AM
Cannot start Tomcat and get "The BASEDIR environment variable is not defined correctl thomas2004ch Linux - Software 1 07-14-2010 05:07 AM
Neither the JAVA_HOME nor the JRE_HOME environment variable is defined xpucto Linux - Newbie 7 06-21-2010 05:30 AM
how to unset environment variable in bash suneel Linux - Newbie 7 09-14-2009 10:17 AM
# ./ Neither the JAVA_HOME nor the JRE_HOME environment variable is defined Niceman2005 Linux - Newbie 3 06-29-2005 01:58 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:46 AM.

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