I want to know how the standard environment variables given by the Linux OS like PATH, HOME are set automatically. Which file(s) are these read from. There should be some file from which these variables are set when a particular user logs in.
3 Answers
I would like to add a few more details to what @cnicutar has already mentioned.
Environment variables including PATH can be:
- System wide - The values of the environment variables last till the system is up
- Session wide - Lasts till a session lasts (till user logs out)
/etc/profile is meant for system settings for Bourne & Bourne compatible shells. The behavior of /etc/profile may vary across distributions.
For the latest Ubuntu distributions, it is recommended to use /etc/environment for system-wide settings. It is not recommended to use /etc/profile or /etc/bash.bashrc as noted the Ubuntu help.
On Ubuntu machines, /etc/profile is a shell script which sources the scripts in /etc/profile.d and the system-wide bashrc file in /etc/bash.bashrc, whereas /etc/environment is a text file consisting of variable assignments per line which are set into the system-wide environment.
For each user the values of environment variables including PATH (for the shell) can also be manipulated through ~/.profile, ~/.bash_profile, ~./bash_login, and ~/.bashrc where ~ means the user's home directory, like /home/alex/.
To see your current environment variables and their values, you can use printenv.
You can refer to the following link for more details on environment variables on Ubuntu systems: https://help.ubuntu.com/community/EnvironmentVariables
4 Comments
source or . or equivalent could work but AFAIK it would be only for that shell wherein you invoke it. The purpose of /etc/environment AFAIK in for system wide setting so a better option probably would be log out - log in.There's nothing magic about them, the shell sets them when it starts up.
You should start reading /etc/profile and work up from there. Alternatively, strace might show you what files the shell tries to read when it starts.
For instance, here is how my /etc/profile starts:
PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin"
export PATH
After that is sources other files which in their turn modify PATH.
Comments
You can even set and manage your environment variables by yourself by using module enviroment software http://en.wikipedia.org/wiki/Environment_Modules_(software). To install "module" you can use this command for Centos
sudo yum install environment-modules
For Ubuntu users, you can install it from source: http://modules.sourceforge.net/
Write a module file to set or prepend environment variables. Then you modules to load this file. For example, my files name "mypath"
proc ModulesHelp { } {
global version prefix
puts stderr "\t Loads the environment for my installed home folder HOME/local"
}
module-whatis "Loads the environment for my installed home folder HOME/local"
set HOME /home/svu/a0081742
prepend-path PATH $HOME/local/bin
prepend-path LIBRARY_PATH $HOME/local/lib
prepend-path LD_LIBRARY_PATH $HOME/local/lib
prepend-path LD_INCLUDE_PATH $HOME/local/include
prepend-path MANPATH $HOME/local/share/man
Then, you load the file with
module load mypath
Than you can list your loaded modules with
module list