jcm
jcm

Reputation: 5659

Why does my $PATH grow every time I run source?

I have noticed that every time I type:

source ~/.bash_profile

in my bash terminal, my $PATH variable keeps getting appended to.

Why is this, and how do I stop it from happening? I just want my current terminal to pick up any changes I have made to my .bash_profile file

Upvotes: 1

Views: 349

Answers (1)

glenn jackman
glenn jackman

Reputation: 247162

I put this in my profile

# This is in /etc/profile
pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            [ ! -d "$1" ] && return
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}

pathmunge /some/nifty/dir before          # not: PATH=/some/nifty/dir:$PATH
pathmunge /a/less/interesting/dir after   # not: PATH=$PATH:/a/less/interesting/dir

That only adds stuff to the path if it's missing.

Upvotes: 2

Related Questions