This is a compilation of tips for bash initialization scripts.

1. Start by unsetting aliases and functions

Useful to avoid recursive alias/function definitions.

# clean aliases
/usr/bin/unalias -a

# clean functions
unset -f -- $(declare -F | cut -d' ' -f3)

Question: why to use /usr/bin/unalias -a instead of unalias -a?

2. Ensure that bash is running interactively

For security reasons only source bashrc when the shell is interactive:

# If not running interactively, don't do anything
[[ $- =~ i ]] || return

3. Defining the PATH

One of the most important variables in bashrc is PATH. I define it as follows:

[ -v INITPATH ] || export INITPATH=$PATH
read -d '' PATH <<-end
    $HOME/.rvm/bin:
    $HOME/.gem/ruby/bin/bin:
    $HOME/perl5/bin:
    $HOME/usr/bin/:
    $HOME/usr/local/bin/:
    $HOME/.local/bin/:
    $HOME/bin/:
    /usr/bin/:
    /usr/sbin/:
    /usr/local/bin/:
    $INITPATH:
end
export PATH=${PATH//[[:space:]]/}

The first line prevents appending repeated directories to PATH when sourcing bashrc.

4. Don’t export builtin redifinitions

Once upon a time I thought this was great idea:

cat() {
  # add features to `cat`
}
export -f cat

But that last line is really bad. Exporting a redifined builtin function can break software, like configure scripts, that depend on that function.