When a bash shell starts several scripts are executed automatically. You can put whatever commands you want to have executed automatically there.
First, a (general) script is executed: /etc/profile
. This is done for every user and therefore it should contain only settings/actions which should be equal for every user logging on.
Next a user-specific script is called: ~/.bash_profile
(if the shell is called as bash
) or ~/.profile
(if the shell is called as sh
). If you do not want to differentiate between these two you can create a symbolic link from one to the other.
These two files are executed for so-called "login-shells": if you connect to a system, identify yourself as a certain user, then a login-shell is started. Which one that is is determined in /etc/passwd
, where the attributes of all users are stored. Here is an example line from a Linux system:
root:x:0:0:Superuser:/root:/bin/bash
It says that the user root
needs to identify with a passwort (the x
- the password itself is stored elsewhere), has the user ID 0, its primary group has the group ID 0, the users description is Superuser
, its home directory is /root
and its login shell is /bin/bash
.
There are other (non-login) shells, which are i.e. started when you start an xterm
(or konsole
or another terminal emulator) in a graphical environment. These shell instances do NOT execute the files mentioned before but they read and execute a file called ~/.bashrc
.
So, here is a simple what-is-where:
/etc/profile
Put in there whatever every user should have set/executed.
~/.profile
Put in there what the respective user should have executed upon login. To set the login shells environment to the same as every other shells environment (if this is desired) add as the last line of this script the lines:
ENV=~/.bashrc
. ~/.bashrc
This will execute ~/.bashrc
for every login script. Notice the dot in front!
~/.bashrc # WRONG
. ~/.bashrc # CORRECT
This means the script is executed not in a subshell but in the same shell! Otherwise whatever you would set in there would be lost when the script ends!
~/.bashrc
Put everything in there what you want to have executed in every shell. This file is also on a per-user basis. Notice that you already have a certain environment in a shell once it gets to executing this script. i.e. you want to do:
export PATH="$PATH:/some/added/path"
because if you do this:
export PATH="/some/added/path"
Whatever the PATH was before it would be reduced to the one directory mentioned.
I hope this helps.
bakunin