1

Currently, my $PATH has two identical entries for /usr/local/bin and I'm curious whether there's a way to clean that up, or if I should just not worry about it. It looks like this (broken up into a readable list):

/Users/myself/.rvm/gems/ruby-2.0.0-p353/bin
/Users/myself/.rvm/gems/ruby-2.0.0-p353@global/bin
/Users/myself/.rvm/rubies/ruby-2.0.0-p353/bin
/usr/local/bin <--( Here )
/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin <--( Here )
/opt/X11/bin
/usr/texbin
/Users/myself/.rvm/bin
ivan
  • 1,878

2 Answers2

4

This is because you have modified something somewhere and have added the same path twice. I avoid this by using a nice little function do modify my $PATH that will only add a directory if that s not already present:

pathmunge () {
        if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
           if [ "$2" = "after" ] ; then
              PATH=$PATH:$1
           else
              PATH=$1:$PATH
           fi
        fi
}

I have that function in my .profile file and define my path as:

pathmunge $HOME/bin
pathmunge $HOME/scripts
export PATH

Just go through your shells initialization files and use this method to avoind duplicates.

If you just want to quickly remove existing dupes, do this:

PATH=$(echo $PATH | tr ':' '\n' | perl -lne 'chomp; print unless $k{$_}; $k{$_}++' | tr '\n' ':' | sed 's/:$//')

I am using perl instead of sort -u to keep the order of the directories unchanged.

terdon
  • 242,166
1

One way to do it, is just redefine your path

export PATH=/Users/myself/.rvm/gems/ruby-2.0.0-p353/bin:
/Users/myself/.rvm/gems/ruby-2.0.0-p353@global/bin:
/Users/myself/.rvm/rubies/ruby-2.0.0-p353/bin:
/usr/local/bin:
/usr/bin:
/bin
/usr/sbin:
/sbin:
/opt/X11/bin:
/usr/texbin:
/Users/myself/.rvm/bin:

(You'll need to put all of this on one line.)

Though having a path defined twice won't hurt anything. Fixing it just depends how OCD you are.

To make this persist across reboots & new terminal windows, you'll need to modify your startup script ( ~/.profile, ~/.bashrc, /etc/environment )

spuder
  • 18,053
  • This is gold: "To make this persist across reboots & new terminal windows". Just spend an hour trying to figure out why entries in $PATH were in triplicate in iTerm. A restart fixed it. – lukeaus Aug 14 '17 at 10:54