Bash Override Built in Methods

Bash, override built in method

It turns out you can override a built in method, quite simple actually.

function cd {
    # actually change the directory with all args passed to the function
    builtin cd "$@"

    # We get access to OLDPWD and PWD.
       echo "Changed directories from '${OLDPWD}' to '${PWD}'."
}

If you add the following to your ~/.bash_profile it will always be in effect for interactive shells.

To find the current cd definition:

shopt -s extdebug ; declare -F cd
#cd 18 ~/.rvm/scripts/cd

rvm overrides the built in cd command. If you want to add your custom method and keep rvmrc working, you can use the provided hooks. Place your script ~/.rvm/hooks/after_cd

As part of the rvm installation process you need to source the script, adding the following to your .bash_profile:

[[ -s $HOME/.rvm/scripts/rvm ]] && source $HOME/.rvm/scripts/rvm

Note that the override will not work with popd and pushd.