Differences between revisions 5 and 6
Revision 5 as of 2008-07-31 17:12:48
Size: 6415
Editor: ?AnttiM
Comment:
Revision 6 as of 2008-07-31 17:14:17
Size: 6434
Editor: ?AnttiM
Comment:
Deletions are marked like this. Additions are marked like this.
Line 44: Line 44:
Boot process in regards of environment variable definition when GUI (or more exactly, a window manager) is used. Boot process in regards of environment variable definition when graphical login is used. (Information here is Gnome / GDM specific)

[:DebianWiki/EditorGuide#translation:Translation(s)]: none

(!) [:/Discussion:Discussion]


General

Environment variables are named strings available to all applications. Variables are used to adapt the application behavior to the environment it is running in. A very common use is to define paths for files, language options etc. Applications query values with getenv()-function specifying the variable name as parameter. Variable value is returned by the system. In other words, application defines what values it reads. Please refer to application manual to see what variables are used by each application. That said, there are several variables that can be considered as standard at least in Linux environments:

  • PATH = Colon separated list of directories to search for binaries.
  • HOME = Current user's home directory.
  • USER = Current logged in user's name.
  • SHELL = The current shell.
  • PS1 = Defines shell's command prompt.
  • (please feel free to add more)

To see your currently defined variables, open up your terminal and type command env

Variables are defined with name-value pair: "NAME = any string as value". Variable name is usually in capital letters. Anything that follows the equal-sign is considered variable's value until terminating line feed character. Whitespace around the equal-sign is ignored. Variables can be defined add-hock in terminal by writing the appropriate command. In Bash this would be 'export MYVAL=Hello world'. In this case variable stays defined until the end of terminal session.

When working in shells or shell scripts: If you do not want to over-write the previous value of the variable, include the variable name into new definition. E.g. in Bash: export PATH=$PATH:~/bin. Sample shows how to append bin-directory in user's home directory into the PATH environment variable.

In most cases it is most convenient to store these variables into a configuration file that is read during system boot and user login so that they are available automatically. Unfortunately this not always as easy as it sounds. Why? For couple of reasons:

  1. Environment variables are inherited i.e. parent program sets the environment for the child process. You need to configure parent's settings so that it passes it on for all it's children.
  2. Various shells and window managers are the parent programs we are looking for but each of them reads a different configuration file (dot file) when it starts.

So, with this knowledge we understand that we need to consider both the starting order of system processes and the configuration files they read when they are started. Lets get to it! There are two ways you can run your Linux box: from text console or graphical user interface.

Using text console

Boot process in regards of environment variable definition when text console (also called login shell) is used.

  1. At the end of boot the mother of all processes init is started. init's environment, including PATH, is defined in source code and cannot be changed at run time.

  2. init runs the start-up scripts from /etc/init.d depending on the run level set in /etc/inittab. Since init's environment is very bare, the scripts define required environment variables within themselves.

  3. init starts the text login process that waits for the user to log in. When user logs in login process checks from the /etc/passwd what shell should be started for this particular user.

  4. Shell starts and reads shell specific configuration files.
    1. Bash reads first /etc/profile for values that are defined for all users. After reading that file, it looks for ~/.bash_profile', ~/.bash_login', and `~/.profile', in that order, and reads and executes commands from the first one that exists and is readable. b. (please fill in other shells as well)

Now the environment variables are ready to be used by the applications you start from the terminal.

Note 1 - If you start X-windows = GUI from this text console your environment variables are already defined as explained above. However, window manager possibly reads same files again (see below). This is usually not detrimental but you may get unexpected results such as PATH having all entries listed twice.

Note 2 - If you start another shell within the first one (yes it is possible), the second one is a non-login shell. It will not read named start-up files but searches non-login start-up script from user's home directory instead. With Bash it is called ~/.bashrc. To avoid specifying same values in two places usually the login-start-up script ~/.bash_profile includes the ~/.bashrc at the end of its execution. Here is how:

if [ -f ~/.bashrc ]; then
   . ~/.bashrc;
fi

Using graphical UI

Boot process in regards of environment variable definition when graphical login is used. (Information here is Gnome / GDM specific)

  1. At the end of boot the mother of all processes init is started.

  2. init runs the start-up scripts from /etc/init.d depending on the run level set in /etc/inittab. Since init's environment is very bare, the scripts define required environment variables within themselves.

  3. Init starts the GDM window manager, which in turn will start the graphical login.
  4. When user successfully logs in the GDM starts xsession. At the beginning of session /etc/gdm/Xsession script is read and with it the environment variables for user's session. The default version of Xsession file reads first /etc/profile for global settings and then ~/.profile to add user's settings.

Now the environment variables are set and used when programs are run in this session.

Note - If you start terminal / console window in graphical desktop environment it will be so called non-login terminal and it will read only the user's non-login start-up script. For Bash this is ~/.bashrc.

Quick guide

For the hasty ones that just need to get the system running here is what you could do:

  • Put all global definitions, i.e. ones affecting all users into /etc/profile.

  • Insert all personal definitions into ~/.profile

  • Create or edit file ~/.bash_profile and include commands:

     if [ -f ~/.profile]; then
         . ~/.profile
     fi


CategorySystemAdministration