Mike Miller <mbmiller+l at gmail.com> wrote:
> This was bugging me for years.  I couldn't understand why I had to
> source ~/.bash_profile to get my path updated on some of my machines
> and not others.

The following blog post breaks down the .bash_profile v.s. .bashrc
desicion quite well:

  http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html

tl;dr: Use ~/.bash_profile to execute code only once per login and use
~/.bashrc to execute for every new /bin/bash instance that
starts.

Terminal emulators do not start as login shells, which IMHO is the Right
Thing To Do(tm). A login shell was traditionally reserved for telnet or
terminal sessions, you know... the old amber or green CRTs with funky
UNIX keyboards, or now with ssh.  When you're using an X11 window
manager, you've used a different method to log in, and the login shell
isn't generally needed.

In practice, there is very little you'll want to do at login that you
wouldn't want to do in every instance of /bin/bash. For that, people use
the pattern of sourcing their ~/.bashrc inside their ~/.bash_profile. An
example:

# ~/.bash_profile
#
# Execute commands only upon login...
#
###########################################################################
# Pull in my ~/.bashrc if I have one for things that must happen for
# every /bin/bash instance. (PATH is set in there...)
###########################################################################
if [[ -f ~/.bashrc ]] ; then
   . ~/.bashrc
fi

###########################################################################
# Make sure I'm in an interactive login before printing to stdout/stderr.
# Otherwise, I'll mess up my ssh sessions.
###########################################################################
if [[ $- =~ 'i' ]] ; then
   # Display uptime
   uptime

   # sysnews package - display new system news since last login
   news
   
   # Do I have any screen sessions open?
   screen -ls
fi

###########################################################################
# Run these commands even if I'm not logging in interactively
###########################################################################
# Clean up my old temp files if enabled
if [[ -f ~/.bash.d/purge_tempfiles ]] ; then
   find $TMPDIR -owner $USERNAME -mtime +1 -delete &
fi
# EOF

And the bashrc file...

# ~/.bashrc
#
# Execute these commands in all BASH shells, incuding all instances of
# shell created for screen/tmux sessions, non-interactive logins over ssh,
# etc.
###########################################################################
# Set up my path...
PATH=".:${HOME}/bin:${HOME}/bin/$(uname -m):/usr/local/sbin:/usr/sbin:/sbin:$PATH"
export PATH

# Loosen up security on my files in general - let my co-workers peek
# behind the curtain
umask 022

# I may not want a DISPLAY from this host
if [[ -f ~/.nodisplay-$(hostname -s) ]] ; then
   unset DISPLAY
fi

###########################################################################
# Make sure I'm in an interactive login before printing to stdout/stderr.
# Otherwise, I'll mess up my ssh sessions.
###########################################################################
if [[ $- =~ 'i' ]] ; then
   # create a directory for other cool interactive customizations
   [[ ! -d ~/.bash.d ]] && mkdir ~/.bash.d
   for rc in ~/.bash.d/*.bash ; do
       . $rc
   done
   biff y
   mesg y
else
   # Non-Interactive shell commands
   biff n
   mesg n
fi
# EOF

I sometimes like to test whether or not an application exists before I
decide to customize my shell for it. For example, the nmh package for
email management may or may not be installed. Before I go about setting
up bash completion or aliases, I check for a well-known nmh script:

# ~/.bash.d/mh.bash
#
if (type -p sortm >/dev/null) ; then
   # MH is installed! Get busy!
    alias sortms="sortm -textfield subject -limit 0"
    alias catchup="mark -seq unseen -del all"
    alias clrf="$mhdir/folder -list"
    alias lsf="$mhdir/folder -list"
    alias puf="$mhdir/folder -push"
    alias pof="$mhdir/folder -pop"
    alias com="$mhdir/anno -component X-comment -text"
    alias lcom="$mhdir/scan;$mhdir/anno -list -number -component X-comment"

    # Update the folder list and template files if the date-stamp file
    # is empty.  (This is done in a cron job each night.)
    if [ ! -s ${HOME}/Mail/date-stamp ] ; then
        (cd ${HOME}/Mail; make) >/dev/null 2>/dev/null
    fi

    do_incprocmailfail() {
        inc=/usr/bin/mh/inc
        for i in ~/.procmailrc $inc ; do
            test -f $i || return 1
        done

        eval $(grep -E "^FAILDROP=" ~/.procmailrc)
        test [ ! -f ${FAILDROP} ]  && return 1

        echo -n "Incorporating failed procmail messages..."
        exec $inc -zero -file ${FAILDROP} +inbox
        echo done.
    }
fi
# EOF


Enjoy!

Chad

-- 
Chad Walstrom <chewie at wookimus.net>
http://runswithd6s.blogspot.com/