How are a user's environment variables set?

Well, this is for knowledge’s sake. I had thought that environment variables are stores somewhere in a file, but recently read that they are created at runtime.

Now coming to openSUSE, even when I boot to runlevel 1 and login ( something like ‘su -l “username”’ ) , I see my environment variables including WINDOWMANAGER etc get set. So, I guess ‘login’ does that. But the login man page is quiet about this.

How are environment variables set? where are the default values taken from?

Thanks in advance.

I think it is started by the script /etc/profile, which you should not edit. That file suggests you can create a /etc/profile.local for custom system wide values. For your own personnel use, you can place them in the file ~/.profile (your /home/username folder). There is also the file, which is not a script where these values can be placed in /etc/environment as well. You also have the script the local ~/.bashrc that is run where you could export environment changes and it runs each time you open up a terminal session. There may be other files as well I am not aware of. To look at and edit these files, when correct to do so, look at my blog here: SYSEdit - System File Editor - Version 1.00 - Blogs - openSUSE Forums

Thank You,

On 2012-03-23 02:56, pbhat wrote:
>
> Well, this is for knowledge’s sake. I had thought that environment
> variables are stores somewhere in a file, but recently read that they
> are created at runtime.

Both are true :slight_smile:

> Now coming to openSUSE, even when I boot to runlevel 1 and login (
> something like ‘su -l “username”’ ) , I see my environment variables
> including WINDOWMANAGER etc get set. So, I guess ‘login’ does that. But
> the login man page is quiet about this.
>
> How are environment variables set? where are the default values taken
> from?

Some come from “/etc/bashrc”. There is another place, let me see if i can
find it again… There is “/etc/environment”. There is another directory
that holds a file for each different shell and which I can’t find now…
ah! yes, “/etc/profile” file and "/etc/profile.d/ directory.


Cheers / Saludos,

Carlos E. R.
(from 11.4 x86_64 “Celadon” at Telcontar)

As others already said, environment variables can be set in several places at system or user level. The one you’re refering to is set at X session start:


**$ grep WINDOWMANAGER /etc/X11/xinit/xinitrc.common** 
test -n "$WINDOWMANAGER" && WINDOWMANAGER="$(type -p $WINDOWMANAGER 2>/dev/null)"
if test -z "$WINDOWMANAGER" ; then
        WINDOWMANAGER=$(type -p kde)
        WINDOWMANAGER=$(type -p startkde)
        WINDOWMANAGER=$(type -p fvwm2)
            WINDOWMANAGER="$(type -p $i 2>/dev/null)"
            test -n "$WINDOWMANAGER" && break
if test -z "$WINDOWMANAGER" ; then
WINDOW_MANAGER=$WINDOWMANAGER
STARTUP=$WINDOWMANAGER
**export WINDOWMANAGER** WINDOW_MANAGER STARTUP

Notice that users can also explicitely set and export WINDOWMANAGER and/or WINDOW_MANAGER (for example on the command line) to select the wm to use before starting X with startx - although nobody does that anymore nowadays, since the preferred way is to use a session manager (such as gdm, kdm, xdm, etc), where the wm is provided by the value of the Exec key of the session desktop file.

You should undertstand the difference between the environment variables belonging to a process (they are not in a file, but belong to a process and that is something that is in memory) and the several files that contain (mostly bash) statements to set/unset these variables.

You should also understand the difference between variables being set and available for use inside a shell (bash) process (a running bash script) and those that are in the process environmnet and also available through the same means in such a shell process.

Sounds difficult and it is, as long as one does not understand (as with so many things ; ).

First, what is a process? It is an instance of an executable being executed. That is it occupies some space in memory and has a program counter that points to the place where the execution is. The place in memory is ocupied by several things. Amongst them the code to be executed and the data used. But also a part in memory contains a list of variables and their values. These are called the environment. A process is allways started from within another process (the parent) and to begin with it inherits the environment of the parent. At start the kernel also contributes some values (amongst other things, the name the program is called with, and the parameters added to the call). These environment parameters can be changed from with the child process. And when it calls another (granchild) process, these changed values are given to it.

Now the shell script (we tak bash here). A process can be a running shells cript. A shell script is written in a programming language that is not compiled, but interpreted, that is, the statements are read and executed from the script file. In every programing language you can use variable to store data. In bash you do this by typing things like:

MYAGE=16

You can then use that variable in other statements like

echo $MYAGE

Now bash has the feature that it also makes available the environment variables of the running process (the script) in the same way. Thus you can do

echo $PWD

to see your current workind directory.
So it is easy to get the environment variable from the environment and also easy to change them:

henk@boven:~> echo $PWD
/home/henk
henk@boven:~> PWD=/home/henk/Documents
henk@boven:~/Documents>

As you see I changed de variable with the immediate effect the same as if I had done

cd Documents

Some environment variables are read-only (else you could do very strange things).

But how can you add a new one to the envirionemnt?
Simply saying

MYAGE=16

does not work, MYAGE is then only available with value 16 in the running script.
But

export MYAGE=16

does place MYAGE in the envirionment. Once it is there and you want to change the value there is no need to use export again.

I hope you now understand what all sorts of asignments in startup sequences as .profile, .bashrc and the like do. They either change variables in the environment (when they are allready there) or create new ones (with *export, *but using export does not mean it wasn’t there before).

And never forget that those environment variables are inherited by offspring processes (never backwards up to he parents of course).
Thus when you say:

export DISPLAY=othersystem.other.domain:0
xclock

You will start the X program xclock not on your own display, but on the display of system othersystem.other.domain (when the security settings of the network and the other system allow). Beacuse when the xclock process starts, it will see in it’s herited environment where it has to open it’s window.

Oh Boy! What a wealth of information in one short post!

I had to smile at your comment:

>Sounds difficult and it is, as long as one does not understand (as with so many things ; ).

How very true!

Don’t want to hijack the thread, just wanted to let you know your efforts were very much appreciated! Thanks a bunch!

You are welcome. And the comment you cite was ment to give the message that it all should not be taken to serious.
But when you have further quetions, please feel free.