Reputation: 2743
I found how bash read startup files :
When Bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. 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. The --noprofile option may be used when the shell is started to inhibit this behavior.
http://www.gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files
Why is that - I mean this queue of "~/.bash_profile, ~/.bash_login, and ~/.profile". (And this logic - "if one of this file exist the other ones are not read at all")
I really don't understand point of that, why we need that much mess. Why Bash don't just read just one "global" and one "user specific" startup file ?
Upvotes: 1
Views: 204
Reputation: 328644
The reason for this is that there are different ways to use a shell, there are different shells and you may want to share / re-use some options (or not!).
For example, all shells derived from Bourne Shell read ~/.profile
. So if you want to share options between /bin/sh
, /bin/ksh
and /bin/bash
, put them there.
But then, you may want different options for BASH and KSH. In that case, use .bash_profile
and .kshrc
respectively and have them source the common ~/.profile
.
Using the rules above, you can fine-tune your shell's setup. It will first load the config file which is most suitable for its purpose. In said config file, you can then chose to load others to inherit whatever your want. If you only use .profile
, then that makes it easy to switch between different shells.
I'm not sure about the difference between .bash_profile
and .bash_login
; maybe this is a leftover from a bug or a change in the design.
Login scripts are executed only for login shells (i.e. the first shell a system creates when a user logs in; all other shells and processes will be children of it). The login shell contains things like global variables which you want everywhere. A common example is the ID of the SSH agent so you can load keys in any shell and they will work for every process of the same user. It doesn't make sense to do that for every shell that you start.
On the other hand, it doesn't make sense to define a prompt for non-interactive shells, so this goes into a different config script.
Upvotes: 2
Reputation: 6667
Bash has a number of different ways of being started and each of these allows for different configuration. These include, interactive, non-interactive, login, non-login, sh and any combination of these.
You are possibly confusing what would be easier for you and what would be easier for someone else with different requirements. This is pretty much the linux / unix way.
EDIT:
The reason for the loading order of the files is that .bash_login and .profile are synonyms for .bash_profile. These come from C shells .login file and bourne shell and korn shells .profile. As I understand it this ordering allows for backward compatibility (unsuccessful in the case of C shell) with these other shells.
Upvotes: 1