4

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?

0

4 Answers 4

4

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.

4
  • @MichaelDurrant NP, happy to help.
    – chown
    Oct 18, 2011 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, 2011 at 20:03
  • @ThomasAndrews Correct, unless you do source .bashrc.
    – chown
    Oct 18, 2011 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, 2011 at 20:26
1

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.

0

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
2
  • sure, but what is a PROFILE? Oct 18, 2011 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, 2011 at 20:15
0

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.)

3
  • 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. Oct 18, 2011 at 19:59
  • Ah, of course. My Windows knowledge has withered to dust. :)
    – Thomas Andrews
    Oct 18, 2011 at 20:01
  • Sadly, some things are hard to forget no matter how hard you try :) Oct 18, 2011 at 20:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .