Wednesday, January 16, 2008

how does bash work

When evoked the Borne Again Shell can launch one of two different types of interactive shells, login or non-login.

These descriptions are based on my experiences with a variety of Debian & Fedora based environments - each has their own peculiarities in their implementation of bash. I'll do my best to be "generic"

When you login via tty, ssh, or su --login - your $SHELL is launched for you. This is called a "login" shell, and it will automatically source the following files:
1) /etc/profile
2) ~/.profile or ~/.bash_profile
(but generally not both, one will take "precedence")
When you launch /bin/bash by some other means (xterm, konsole, su, or even typing /bin/bash) it is considered non-login and it WILL source .bashrc

It is VERY common for ~/.profile (or ~/.bash_profile) to source ~/.bashrc by either:
. ~/.bashrc
source ~/.bashrc
You can use source either way - in both interactive and non-interactive shells.

/etc/profile may source lots of other files. Sometimes /etc/bashrc (or /etc/bash.bashrc) and sometimes even ~/.bashrc - so watch out!

Your system may not have all these files - or they may have different names. Try:
ls -al /etc/ | grep bashrc
ls -al /etc/ | grep profile
ls -al ~ | grep bashrc
ls -al ~ | grep profile
to get some different ideas.

the command "su -" is short hand for "su --login root" or "switch user to root and launch a login shell"
it is NOT "switch to super user and load environment variables" (even if that may be the case on some systems)

No comments: