*nix shell profiles – order is important!

Often I hear the question “How should I update my profile?” and then that usually leads to what file should I use.
It is important to understand the file loading order and why this adds flexibility.
My comments here will focus on bash but most/all shells have similar loading features.
When your bored one rainly day do a “man bash”. Be forewarned, this will bring up one of the largest man pages on the system. Specifically, to find information on bash loading, search for the section heading called INVOCATION.
The order here is important
The bash shell on login first reads and executes commands from the file (~=your home dir)
/etc/profile, if that file exists. <- this affects *all* users on the system
After reading that file, it looks for
~/.bash_profile, <-this affects only this user
~/.bash_login, and <- only affects this user
~/.profile, <-- this one is honored by many command shells, a kind of universal source file and reads and executes commands from the first one that exists and is readable.

Many distros will include a source file request within the ~.bash_profile similar to this:

if [ -f ~/.bashrc ]; then
  source ~/.bashrc # use the word “source” not the shortcut “.”
                          # as older bash shells will not know the shortcut.
fi

I even include another one at the end of my .bashrc to look for an include file called ~/.bash_local - for local commands related to that server. For example:

# --- add this at the end of ~/.bashrc ---#
if [ -f $MYHOME/.bash_local ]; then
  source $MYHOME/.bash_local
else
  echo 'PATH=$PATH:$HOME/dev/utils'>>$MYHOME/.bash_local
  source $MYHOME/.bash_local
fi
#---- ~/.bash_local contents ----#
PATH=$PATH:$HOME/dev/utils
alias fin="cd ~/dev/fin"
alias www="cd /var/www/"

Enjoy,
Geoff

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply