Web page archived as of 2018-05-22. Some links & features might not work.

Bash shell startup files in Debian Squeeze

Whenever I need to research which files are actually read or executed to set up the Bash environment at startup my brain refuses to cooperate. One thing is to understand the Bash manual about Startup Files and the Bash man page. That's still somewhat OK, but when I start looking at the actual file contents1) my gray matter's neurons begin to act in weird ways2).

So, I ran my own tests on a fresh new installation of Debian Squeeze. To keep things simple, at first I only looked at shells invoked by ssh logins, su, /bin/bash and such. However, POSIX mode and /bin/sh are mentioned in the overview table. Anyway, here is what the tests boil down to.

Mainly, 4 files are read: /etc/bash.bashrc, /etc/profile, ~/.bashrc, ~/.profile

Login shells

  • Typical commands: ssh user@host, su - user, /bin/bash --login
  • Startup files executed:
    1. /etc/profile, which also sources
    2. /etc/bash.bashrc and then all files matching
    3. /etc/profile.d/*.sh
    4. ~/.profile, which also sources
    5. ~/.bashrc

Interactive non-login shells

  • Typical commands: su user, /bin/bash [-i], "subshells" as sometimes run by programs when they offer shells
  • Startup files executed:
    1. /etc/bash.bashrc
    2. ~/.bashrc

Non-interactive shells

  • Typical commands: ssh [-t] user@host command, /bin/bash -c command, shebang style script shells (#!/bin/bash), and shells as run by programs when they offer shells, e.g. ^Z in nano
  • Startup files executed:

Overview table

Shell type /etc/profile /etc/bash.bashrc ~/.profile ~/.bashrc
Login shells X X X X
Interactive non-login X X
Non-interactive
/bin/bash --posix
/bin/sh --login X X X X
/bin/sh

Non-interactive shells either source $BASH_ENV, or in POSIX mode $ENV.

~/.profile

~/.profile is special because Bash actually looks for ~/.bash_profile, ~/.bash_login and ~/.profile, in this order, and Bash runs only the 1st of these 3 it finds (if it is readable). However, on a fresh Debian Squeeze ~/.bash_profile and ~/.bash_login do not exist; only ~/.bashrc, ~/.profile, and ~/.bash_logout.

1)
Such as if [ "$PS1" ]; then if [ "$BASH" ]; then if [ -f /etc/bash.bashrc] …
2)
I am just trying to express politely what I think :-X
3)
Actually, Bash tries to detect whether it is run by ssh user@host command or not. In case of these "non-interactive login shells" Bash executes /etc/bash.bashrc and ~/.bashrc. But in Debian Squeeze both files quit right at the start due to the fact that their first command is [ -z "$PS1" ] && return.

Discussion

seb, 2012-05-25 12:56

Useful, thank you.

Flink, 2012-07-02 19:02

I work with Unix/Linux for more than 10 years. And stuff like this makes me always cry. Thanks for this time saver!!!

Andreas Schamanek, 2012-07-02 20:31

Thanks for the feedback! I cannot but agree. However, I think this is not a *nix thing. It's like with entropy, if you don't constantly maintain your systems they become more complex all on their own :)

 
blog/110814_bash_shell_startup_files_in_debian_squeeze.txt · Last modified: 2011-08-15 00:32 by andreas