Run a script on login using ~/.bash_login
If .bash_profile exists, then Bash will not read .bash_login (or .profile). This annoying feature is described in some versions of the Bash manual, but not all.
.bash_profile
and .bash_login
are analogous, so I recommend you put your commands in .bash_profile
, because it's is commonly used and .bash_login
is relatively unknown. Also consider putting your commands in .bashrc
instead of .bash_profile
. The manual describes difference between "interactive non-login shell" and "interactive login shell", so be sure to read that section.
The GNU Bash Reference Manual version 4.1: Bash Startup Files says:
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.
Also see this question on superuser, and this Bash howto (Don't be deterred by the freeunix.dyndns.org:8088 address--- it's still a good quality manual and I have used it for years).
Update, since you say you don't have a .bash_profile.
It sounds like you are not using what's called a "interactive non-login shell" (See the Bash manual for a detailed description).
To test this, add something like the following each file: .bashrc
, .bash_profile
and .bash_login
.
echo "DEBUG: I am .bashrc"
echo "DEBUG: I am .bash_profile"
Then log out and log in again. When you log in, I bet you will only see the phrase "DEBUG: I am .bashrc" but not "I am .bash_profile". If so, it means you are a "interactive non-login shell", which simply means that Bash will call .bashrc
but not .bash_profile
. For information why these dotfiles are the way they are, see @Andrejs Cainikovs's post below and http://mywiki.wooledge.org/DotFiles
Login shells, regardless of if they are interactive or non-interactive read and execute the .profile
Interactive shells read and execute .bashrc
.
Often you will see that /etc/profile
sources .bashrc
- thus all settings made in .bashrc
will also take effect in a login shell regardless of whether it is interactive or non-interactive.
The order of execution of the initialization scripts for a shell is dependent on if the shell is interactive or non-interactive and not related to if it is a login script or not.
When bash is invoked as an interactive login shell it reads and executes commands from the /etc/profile
. Then Bash will then try in order to execute ONLY the first file exists and is readable of the following:
.bash_profile
.bash_login
.profile
If one of these files is found but can not be read, it will cause an error. There is no error if any are NOT found.
This same process is followed when a non-interactive login shell is invoked with the --login
option.
Bash only looks for .bash_login
or .profile
files if it is executed as interactive login shell. When it is executed as interactive non-login shell it reads .bashrc
.
Commonly it is the second case, i.e when you run gnome-terminal
bash is run as non-login shell.
This clearly states that if you are booting into Gnome, .bash_login
will not be executed. But if you lower runlevel to boot directly to bash, the same file will be executed upon succesfull login.
I assume .bash_login
will be executed in case of remote SSH connections as well.
Partially ripped from here.