Pushin’ and Popin’ like a pro in Bash

This was blatantly stolen from here.

Ever wish

$ cd -

took you back to the previous directory more than once?

The answer was so obvious once I saw it that I smacked my head and said “D’OH” out loud (causing several people around the office to give me that ‘wth!?’ look.)

Stick this in your .bashrc and be welcomed into the world of directory history:

function cd {
    if (("$#" > 0)); then
        if [ "$1" == "-" ]; then
            popd > /dev/null
        else
            pushd "$@" > /dev/null
        fi
    else
        cd $HOME
    fi
}

I suppose that one could just type ‘pushd’ or ‘popd’ or alias those to shorter commands, but my muscle memory has simply chiseled cd (and ls for that matter) into stone.