LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 03-08-2012, 06:15 AM   #1
acomber
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


Hi

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?

Angus
 
Old 03-08-2012, 08:17 AM   #2
SecretCode
Member
 
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, 08:18 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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, 09:32 AM   #4
devUnix
Member
 
Registered: Oct 2010
Location: Bengaluru, India
Distribution: RHEL 5.1 on My PC, & SunOS / Sun Solaris, RHEL, SuSe, Debian, FreeBSD and other Linux flavors @ Work
Posts: 576

Rep: Reputation: 48
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 09:33 AM.
 
Old 03-10-2012, 10:07 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

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


Reply


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 07:55 AM
Cannot start Tomcat and get "The BASEDIR environment variable is not defined correctl thomas2004ch Linux - Software 1 07-14-2010 06:07 AM
Neither the JAVA_HOME nor the JRE_HOME environment variable is defined xpucto Linux - Newbie 7 06-21-2010 06:30 AM
how to unset environment variable in bash suneel Linux - Newbie 7 09-14-2009 11:17 AM
# ./startup.sh Neither the JAVA_HOME nor the JRE_HOME environment variable is defined Niceman2005 Linux - Newbie 3 06-29-2005 02:58 PM


All times are GMT -5. The time now is 08:29 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