Permanent changes to the environment#
A change or definition of an environment variable is only seen by the current shell and its child processes. It will be lost once the shell is closed. The mechanism to make permanent changes is straight forward. There are several files which will be sourced on login or when creating a new shell. Variables defined in these files will be available for every new shell. There are several files which will be read in different situations.
User specific environment#
File |
Usage |
---|---|
|
Sourced at login, e.g. SSH logins |
|
Alternative for |
|
Common profile, sourced by |
|
Sourced on start of non-login shells, e.g. start of new terminal |
|
Sourced on logout |
Note
It is very common to modify just ~/.bashrc
and reference to it again in ~/.bash_profile
. So a typical ~/.bash_profile
looks like
if [ -f ~/.bashrc ]; then # check if ~/.bashrc exists
source ~/.bashrc # if so, source its content
fi
This way everything from ~/.bashrc
also applies to login shells, e.g. SSH logins.
Systemwide environment#
The files mention in the previous section are located in a user’s home directory and are only consulted by programs run by this user. There are analogous files which apply to every user. They are read before the user profiles, i.e. the latter have a higher precedence as they may override variables. Most notably there are
/etc/bashrc
/etc/environment
/etc/profile.d/*.sh
Actually /etc/profile
is executed on logins and reads all the *.sh
files from /etc/profile.d/
. These files should not be modified as they come with package installations and subsequent updates may delete any changes. Instead new files should be added.
Many distributions also come up with other or more files to restore an environment.