Appending and Prepending to the PATH Variable

I often want to add paths to my $PATH variable.

It’s nice to do this with functions (in your .zshrc or .bashrc file):

pathappend () {
    if [ -d "$1" ] && [[ ":$PATH:" != *":$1:"* ]]; then
        PATH="${PATH:+"$PATH:"}$1"
    fi
}

pathprepend () {
    if [ -d "$1" ] && [[ ":$PATH:" != *":$1:"* ]]; then
        PATH="$1${PATH:+":$PATH"}"
    fi
}

These will check if a) the directory exists and b) it isn’t already in your $PATH, and then either append or prepend the path into your $PATH variable.

Then you can put things like this in:

pathprepend $HOME/.local/bin
pathappend $HOME/code/bin

And if you execute your .bashrc or .zshrc multiple times, your $PATH won’t grow.

Note: The functions above come almost directly from a superuser post.