This question already has an answer here:

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):

/usr/local/bin <--( Here )
/usr/local/bin <--( Here )

marked as duplicate by manatwork, Anthon, slm, rahmu, jasonwryan Jan 19 '14 at 19:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

migrated from superuser.com Jan 19 '14 at 18:16

This question came from our site for computer enthusiasts and power users.

  • Migrated on request of the OP. (Ivan, note that Linux and Unix questions are perfectly on topic for Super User too.) – slhck Jan 19 '14 at 18:16
  • 2
    With zsh: typeset -U path. – Stéphane Chazelas Jan 19 '14 at 18:53
  • 1
    With tcsh: set -f path = ($path:q) – Stéphane Chazelas Jan 19 '14 at 18:55

One way to do it, is just redefine your path

export PATH=/Users/myself/.rvm/gems/ruby-2.0.0-p353/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 )

  • 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

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

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.