*NIX – does exporting a variable only last while machine is on

bash

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?

Best Answer

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.

Related Question