Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I know that I can set a variable, 'export' the variables and put an alias statement in my .bash_rc file.

So:

When I just set a variable it just lasts for that session and is not visible to other sessions, correct?
Then when I export it it it is visible to other shell sessions (is that only to other 'new' sessions though?
Can existing sessions reload or soemthing to get it?)
Most critically does that export 'stay' through a reboot?
Or is that when the .bashrc file comes in for 'really' permanent setting of variables going forward?
Finally any known differences between Ubuntu and Mac on this?

share|improve this question
add comment

migrated from stackoverflow.com Oct 19 '11 at 2:51

This question came from our site for professional and enthusiast programmers.

4 Answers

up vote 3 down vote accepted

You are correct about .bashrc. On the initial boot, the OS knows nothing of environment variables except what it reads in .profilerc, .bashrc, .inputrc, etc. And anything exported only lasts for the current session unless placed in one of those files.

If values are added to one of these files and you want it to take effect immediately, run:

source .bashrc

or:

. /etc/bashrc

etc...


Edit (adding from @ThomasAndrews comments):

However, this only changes the env variables for the current session (terminal window) in which you execute it. The key to understanding is to realize that environment variables are inherited from parent processes to child process at the time the child process starts.

share|improve this answer
    
great, Thx chown! nb: can't accept answer for 9 mins –  Michael Durrant Oct 18 '11 at 19:55
    
@MichaelDurrant NP, happy to help. –  chown Oct 18 '11 at 19:59
    
Just be careful to realize that changing these files does not affect any process that is currently running. It only affects future processes, and it depends on how the processes are started. –  Thomas Andrews Oct 18 '11 at 20:03
    
@ThomasAndrews Correct, unless you do source .bashrc. –  chown Oct 18 '11 at 20:23
    
@chown And yet that only changes in the terminal window in which you execute it. The key to understanding is to realize that environment variables are inherited from parent processes to child process at the time the child process starts, and the only way to change the child process is to explicitly set the variable in that process. –  Thomas Andrews Oct 18 '11 at 20:26
add comment

In *nix, environment variables are on a per-process basis. Child processes inherit their parent process environment variable values at the time the child starts.

So, if you export a variable from a shell, say, then that environment variable affects only that shell process and any processes started from that shell.

This is different from environment variables on Windows, where they are essentially global and permanent when set from the control panel. (Edited per comment below.)

Typically, if you want to set an environment variable for every process that you might start, you place it in your login script, like .bash_profile (if bash is your shell.)

(The .bashrc file gets loaded with every shell, which you might want or you might not want - if you hand-set another value, and then run "bash," do you want .bashrc to override your new value? .bash_profile gets called with the login. Either one could be the correct place to put an environment variable.)

share|improve this answer
    
Windows environment variables are only global if you set them globally via the control panel. If you set environment variables in a cmd window, they will remain local to that window. –  ObscureRobot Oct 18 '11 at 19:59
    
Ah, of course. My Windows knowledge has withered to dust. :) –  Thomas Andrews Oct 18 '11 at 20:01
    
Sadly, some things are hard to forget no matter how hard you try :) –  ObscureRobot Oct 18 '11 at 20:07
add comment

You can add the export statement to your PROFILE ( http://www.hot.ee/airm/linux/BLFS6.3/postlfs/profile.html )

Something like:

echo 'export PATH=/usr/local/my/bin:$PATH' >> ~/.profile
share|improve this answer
    
sure, but what is a PROFILE? –  ObscureRobot Oct 18 '11 at 20:00
    
And, again, this doesn't change any existing process, it just ensures that the environment variable will be set the next time you log in. –  Thomas Andrews Oct 18 '11 at 20:15
add comment

First, you need to determine what shell you are using. Do this:

$ echo $0
-bash

Now check the man pages for your shell:

$ man bash
<man pages>

Scroll down to the bottom of the man page that just loaded, and look for the FILES section. Note that there are different initilization files for different kinds of shells. The files and location will depend on the shell you are using.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.