6

I've been researching about how bash works, and so far I've learned the following things:

When starting a login shell, the first of the following files that exists gets executed:

~/.bash_profile, ~/.bash_login, ~/.profile

When starting a non-login shell (or a sub-shell) that is interactive, the ~/.bashrc file gets executed.

Also, .profile gets executed by other shells like sh. My question now is, what is the point of having a .bash_profile as well as a .bash_login? They both perform the same functions, and unlike .profile, both .bash_profile and .bash_login are both only read by bash. The only difference that I know between them is that .bash_login gets executed if .bash_profile is not present. So why is it there?

6
  • 1
    There is really no point in it. It's a compatibility thing, it has always been that way (eg. back in 1996).
    – mosvy
    Oct 8, 2018 at 21:01
  • Compatibility with what though? Oct 9, 2018 at 1:10
  • And how does having ~/.bash_login help with that Oct 9, 2018 at 1:21
  • I guess some people had their startup file named that way, and in those times randomly breaking people's configs was not yet considered a feature -- but why not ask Chet Ramey himself?
    – mosvy
    Oct 9, 2018 at 1:24
  • Duplicate of this? Also, .bash_login is explained in the manpage as "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"
    – ckujau
    Oct 9, 2018 at 4:38

2 Answers 2

6

The csh shell, from which bash got a few features, uses .login as the name of the shell startup file to run when starting a login shell, just like ksh, another shell that heavily influences the bash shell, uses .profile as the startup file for login shells.

Therefore, .bash_profile borrows its name from the ksh shell's .profile file, while .bash_login borrows its name from the csh shell's .login file.

The user uses .bash_profile or .bash_login depending on from what other family of shells (ksh-like shells or csh-like shells) they are migrating to bash from.

Obviously, nowadays, many Linux users have never used another shell than bash, so whatever filename they use will more likely depend on the preferences of their system administrator, of their teacher, or it may be random chance.

If both ~/.bash_profile and ~/.bash_login exists, then the ~/.bash_login file will be ignored.

1
1

One advantage to using the optional script name ~/.bash_login, rather than ~/.profile, is that there is a parallel name for your logout script, ~/.bash_logout, if you decide to use it.

So if you're running Bash, rather than Bourne, this simplifies the configuration name set complexity from this:

/etc/profile         # system login       script
~/.bash_profile      # user   login       script
~/.bash_logout       # user   logout      script

/etc/bash.bashrc     # system interactive script
       ~/.bashrc     # user   interactive script

to this:

/etc/profile         # system login       script
~/.bash_login        # user   login       script  <--sisters +
~/.bash_logout       # user   logout      script  <----------+

/etc/bash.bashrc     # system interactive script
       ~/.bashrc     # user   interactive script

However, if you don't intend to use a logout script, this other way might be simpler for you:

   /etc/profile      # system login       script
~/.bash_profile      # user   login       script

/etc/bash.bashrc     # system interactive script
       ~/.bashrc     # user   interactive script

Too bad there isn't more name symmetry in the config names...

But you might make this somewhat better with these symbolic links:

/etc/bash   -->    /etc/profile   # system login       script
  ~/.bash   --> ~/.bash_profile   # user   login       script

/etc/bashrc --> /etc/bash.bashrc  # system interactive script
  ~/.bashrc                       # user   interactive script

Non-destructively created with:

sudo ln -sT    /etc/profile  /etc/bash
     ln -sT ~/.bash_profile    ~/.bash

sudo ln -sT /etc/bash.bashrc /etc/bashrc
#                              ~/.bashrc  (don't change)

Or you can get more aggressive and reverse this, and move the files to the new names, and instead back-link them:

sudo mv -f    /etc/profile       /etc/bash          && \
sudo ln -sT   /etc/bash          /etc/profile

sudo mv -f    /etc/bash.bashrc   /etc/bashrc        && \
sudo ln -sT   /etc/bashrc        /etc/bash.bashrc

     mv -f    ~/.bash_profile    ~/.bash            && \
     ln -sT   ~/.bash            ~/.bash_profile

#             ~/.bashrc              (don't change)

Either way, now you have these more uniform config file names, easily found when you forget them from their links:

/etc/bash    # system login       config script
  ~/.bash    # user   login       config script

/etc/bashrc  # system interactive config script
  ~/.bashrc  # user   interactive config script

(I don't use a logout script, but it would probably be named: /etc/bash.logout .)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.