Preventing duplicate entried in PATH (.cshrc)











up vote
3
down vote

favorite












I need to prepend directories to the path variable in my .cshrc file and I want to make sure that the entries are not repeated when compared to existing directories in the path variable. Can someone advise suitable commands for that? The path on my machine is : separated, not space separated.










share|improve this question
























  • I added a reply to complement Stephane answer with a way to "un-duplicate" the PATH automagically (while keeping important binary dirs up front!)
    – Olivier Dulac
    Jun 5 '13 at 15:25

















up vote
3
down vote

favorite












I need to prepend directories to the path variable in my .cshrc file and I want to make sure that the entries are not repeated when compared to existing directories in the path variable. Can someone advise suitable commands for that? The path on my machine is : separated, not space separated.










share|improve this question
























  • I added a reply to complement Stephane answer with a way to "un-duplicate" the PATH automagically (while keeping important binary dirs up front!)
    – Olivier Dulac
    Jun 5 '13 at 15:25















up vote
3
down vote

favorite









up vote
3
down vote

favorite











I need to prepend directories to the path variable in my .cshrc file and I want to make sure that the entries are not repeated when compared to existing directories in the path variable. Can someone advise suitable commands for that? The path on my machine is : separated, not space separated.










share|improve this question















I need to prepend directories to the path variable in my .cshrc file and I want to make sure that the entries are not repeated when compared to existing directories in the path variable. Can someone advise suitable commands for that? The path on my machine is : separated, not space separated.







path csh






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









Rui F Ribeiro

38.2k1475125




38.2k1475125










asked May 30 '13 at 14:40









astralsmith

1612




1612












  • I added a reply to complement Stephane answer with a way to "un-duplicate" the PATH automagically (while keeping important binary dirs up front!)
    – Olivier Dulac
    Jun 5 '13 at 15:25




















  • I added a reply to complement Stephane answer with a way to "un-duplicate" the PATH automagically (while keeping important binary dirs up front!)
    – Olivier Dulac
    Jun 5 '13 at 15:25


















I added a reply to complement Stephane answer with a way to "un-duplicate" the PATH automagically (while keeping important binary dirs up front!)
– Olivier Dulac
Jun 5 '13 at 15:25






I added a reply to complement Stephane answer with a way to "un-duplicate" the PATH automagically (while keeping important binary dirs up front!)
– Olivier Dulac
Jun 5 '13 at 15:25












3 Answers
3






active

oldest

votes

















up vote
3
down vote













If on Linux, I suppose your csh is tcsh. Then you should be able to do:



set -f path=("/new/entry" $path:q)


In csh, tcsh and zsh, the $path special array variable is tied to the $PATH scalar environment variable in that the elements of the $path array are constructed by splitting the $PATH variable on the colon character. Any modification of either $path or $PATH is automatically reflected into the other variable.



-f above is to retain only the first entry. $path:q is the elements of $path, quoted, that is preventing word splitting. So the syntax above prepends the /new/entry or moves it to the front if it was already there.



Why would you be using csh though?





Note: the quotes above are necessary. Or more precisely, all the characters in /new/entry need to be quoted one way or another.



set -f path=('/new/'e"ntry" $path:q)


is OK.



set -f path=(/'new/entry' $path:q)


is not. You can always do it in two stages though:



set path=(/new/entry $path:q)
set -f path=($path:q)


(one of the reasons you may want to stay away from csh)






share|improve this answer



















  • 1




    +1 for the "why would you be using csh though?" : please, all csh users, read : faqs.org/faqs/unix-faq/shell/csh-whynot (or the infamous 'Csh-programming considered harmful')
    – Olivier Dulac
    May 30 '13 at 16:31










  • @OlivierDulac That's an argument against csh for scripts, not against csh for interactive use. The real argument against (t)csh for interactive use is that zsh has surpassed tcsh for 20 years, and bash has surpassed tcsh for 10 years, so you should switch to software maintained this century.
    – Gilles
    May 30 '13 at 22:49










  • Hi Stephane, thanks for a quick answer! The use of csh is not by choice in my case, it is decided by the tools that we use. Can you also suggest a way if the new additions to $PATH are several : delimited directories? Thanks!
    – astralsmith
    May 31 '13 at 7:08










  • @astralsmith, as I said, in (t)csh, the $path array variable is tied to the colon separated scalar variable $PATH. Modifying one will automatically update the other.
    – Stéphane Chazelas
    May 31 '13 at 7:32










  • Thanks Stephane. It works, although the entries are being repeated and not being moved to the front...
    – astralsmith
    May 31 '13 at 7:53


















up vote
0
down vote













I believe that the following can do what you want.



if ( $PATH =~ */some/path* ) then
set PATH = ($PATH:/some/path)
endif


Note: I'm more of a bash user, so if I have a small error let me know
csh if test






share|improve this answer























  • Hi slm, it did not work in my case. I get "Bad : modifier in $ (/).".
    – astralsmith
    May 31 '13 at 11:58


















up vote
0
down vote













(note: i can not test the below right now... hope it works)



I just add this as a complement to stephane answer: how to get rid of duplicates in $PATH



I'll assume you have




  • directories that should stay in front

  • and other directories as well, order not important (ie, placed AFTER the above)


so:



To un-duplicate entries:



UNIQUE_LIST=$( echo "$PATH" | tr ':' 'n' | sort | uniq)

# then we place in front those from UNIQUELIST that match an ordered list
# note that that way, those who didn't have "/sbin" still won't have it, but if they did
# it will be at the right place in the list
shouldbefirst="/bin /sbin /usr/bin" # complete or re-order as needed on your system...
for dir in $shouldbefirst
do
if ( echo "$UNIQUE_LIST" | grep "$dir" >/dev/null 2>/dev/null)
then #we have this dir in UNIQUE_LIST
NEWLIST="${NEWLIST}:${dir}"
UNIQUE_LIST="$( echo "$UNIQUE_LIST" | grep -v "^$dir$")" #we treated that one, take it out of the original list
fi
done

# then put the remaining of UNIQUE_LIST in the order you want (here, alphabetically)
for dir in $UNIQUE_LIST
do
NEWLIST="${NEWLIST}:${dir}"
UNIQUE_LIST="$( echo "$UNIQUE_LIST" | grep -v "^$dir$")" #we treated that one, take it out of the original list
done

# get rid of possible first ":" (as NEWLIST starts empty)
NEWLIST="$(echo "$NEWLIST" | sed -e 's/^://')"

# and then : (I test by placing "echo" in front, get rid of "echo" if it looks fine)
echo PATH="$NEWLIST"


(once again, i can not test right now, but hope it helps)



NOTE: I'll add recommendation: get rid of "." in your PATH, as it will be "bumped" up to just after the SHOULDBEFIRST dirs ... ("." should always be avoided, and if used, always in the last place only, so that you can't easily bypass commands from /bin, /usr/bin, etc)






share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f77661%2fpreventing-duplicate-entried-in-path-cshrc%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote













    If on Linux, I suppose your csh is tcsh. Then you should be able to do:



    set -f path=("/new/entry" $path:q)


    In csh, tcsh and zsh, the $path special array variable is tied to the $PATH scalar environment variable in that the elements of the $path array are constructed by splitting the $PATH variable on the colon character. Any modification of either $path or $PATH is automatically reflected into the other variable.



    -f above is to retain only the first entry. $path:q is the elements of $path, quoted, that is preventing word splitting. So the syntax above prepends the /new/entry or moves it to the front if it was already there.



    Why would you be using csh though?





    Note: the quotes above are necessary. Or more precisely, all the characters in /new/entry need to be quoted one way or another.



    set -f path=('/new/'e"ntry" $path:q)


    is OK.



    set -f path=(/'new/entry' $path:q)


    is not. You can always do it in two stages though:



    set path=(/new/entry $path:q)
    set -f path=($path:q)


    (one of the reasons you may want to stay away from csh)






    share|improve this answer



















    • 1




      +1 for the "why would you be using csh though?" : please, all csh users, read : faqs.org/faqs/unix-faq/shell/csh-whynot (or the infamous 'Csh-programming considered harmful')
      – Olivier Dulac
      May 30 '13 at 16:31










    • @OlivierDulac That's an argument against csh for scripts, not against csh for interactive use. The real argument against (t)csh for interactive use is that zsh has surpassed tcsh for 20 years, and bash has surpassed tcsh for 10 years, so you should switch to software maintained this century.
      – Gilles
      May 30 '13 at 22:49










    • Hi Stephane, thanks for a quick answer! The use of csh is not by choice in my case, it is decided by the tools that we use. Can you also suggest a way if the new additions to $PATH are several : delimited directories? Thanks!
      – astralsmith
      May 31 '13 at 7:08










    • @astralsmith, as I said, in (t)csh, the $path array variable is tied to the colon separated scalar variable $PATH. Modifying one will automatically update the other.
      – Stéphane Chazelas
      May 31 '13 at 7:32










    • Thanks Stephane. It works, although the entries are being repeated and not being moved to the front...
      – astralsmith
      May 31 '13 at 7:53















    up vote
    3
    down vote













    If on Linux, I suppose your csh is tcsh. Then you should be able to do:



    set -f path=("/new/entry" $path:q)


    In csh, tcsh and zsh, the $path special array variable is tied to the $PATH scalar environment variable in that the elements of the $path array are constructed by splitting the $PATH variable on the colon character. Any modification of either $path or $PATH is automatically reflected into the other variable.



    -f above is to retain only the first entry. $path:q is the elements of $path, quoted, that is preventing word splitting. So the syntax above prepends the /new/entry or moves it to the front if it was already there.



    Why would you be using csh though?





    Note: the quotes above are necessary. Or more precisely, all the characters in /new/entry need to be quoted one way or another.



    set -f path=('/new/'e"ntry" $path:q)


    is OK.



    set -f path=(/'new/entry' $path:q)


    is not. You can always do it in two stages though:



    set path=(/new/entry $path:q)
    set -f path=($path:q)


    (one of the reasons you may want to stay away from csh)






    share|improve this answer



















    • 1




      +1 for the "why would you be using csh though?" : please, all csh users, read : faqs.org/faqs/unix-faq/shell/csh-whynot (or the infamous 'Csh-programming considered harmful')
      – Olivier Dulac
      May 30 '13 at 16:31










    • @OlivierDulac That's an argument against csh for scripts, not against csh for interactive use. The real argument against (t)csh for interactive use is that zsh has surpassed tcsh for 20 years, and bash has surpassed tcsh for 10 years, so you should switch to software maintained this century.
      – Gilles
      May 30 '13 at 22:49










    • Hi Stephane, thanks for a quick answer! The use of csh is not by choice in my case, it is decided by the tools that we use. Can you also suggest a way if the new additions to $PATH are several : delimited directories? Thanks!
      – astralsmith
      May 31 '13 at 7:08










    • @astralsmith, as I said, in (t)csh, the $path array variable is tied to the colon separated scalar variable $PATH. Modifying one will automatically update the other.
      – Stéphane Chazelas
      May 31 '13 at 7:32










    • Thanks Stephane. It works, although the entries are being repeated and not being moved to the front...
      – astralsmith
      May 31 '13 at 7:53













    up vote
    3
    down vote










    up vote
    3
    down vote









    If on Linux, I suppose your csh is tcsh. Then you should be able to do:



    set -f path=("/new/entry" $path:q)


    In csh, tcsh and zsh, the $path special array variable is tied to the $PATH scalar environment variable in that the elements of the $path array are constructed by splitting the $PATH variable on the colon character. Any modification of either $path or $PATH is automatically reflected into the other variable.



    -f above is to retain only the first entry. $path:q is the elements of $path, quoted, that is preventing word splitting. So the syntax above prepends the /new/entry or moves it to the front if it was already there.



    Why would you be using csh though?





    Note: the quotes above are necessary. Or more precisely, all the characters in /new/entry need to be quoted one way or another.



    set -f path=('/new/'e"ntry" $path:q)


    is OK.



    set -f path=(/'new/entry' $path:q)


    is not. You can always do it in two stages though:



    set path=(/new/entry $path:q)
    set -f path=($path:q)


    (one of the reasons you may want to stay away from csh)






    share|improve this answer














    If on Linux, I suppose your csh is tcsh. Then you should be able to do:



    set -f path=("/new/entry" $path:q)


    In csh, tcsh and zsh, the $path special array variable is tied to the $PATH scalar environment variable in that the elements of the $path array are constructed by splitting the $PATH variable on the colon character. Any modification of either $path or $PATH is automatically reflected into the other variable.



    -f above is to retain only the first entry. $path:q is the elements of $path, quoted, that is preventing word splitting. So the syntax above prepends the /new/entry or moves it to the front if it was already there.



    Why would you be using csh though?





    Note: the quotes above are necessary. Or more precisely, all the characters in /new/entry need to be quoted one way or another.



    set -f path=('/new/'e"ntry" $path:q)


    is OK.



    set -f path=(/'new/entry' $path:q)


    is not. You can always do it in two stages though:



    set path=(/new/entry $path:q)
    set -f path=($path:q)


    (one of the reasons you may want to stay away from csh)







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited May 31 '13 at 13:19

























    answered May 30 '13 at 15:11









    Stéphane Chazelas

    294k54554897




    294k54554897








    • 1




      +1 for the "why would you be using csh though?" : please, all csh users, read : faqs.org/faqs/unix-faq/shell/csh-whynot (or the infamous 'Csh-programming considered harmful')
      – Olivier Dulac
      May 30 '13 at 16:31










    • @OlivierDulac That's an argument against csh for scripts, not against csh for interactive use. The real argument against (t)csh for interactive use is that zsh has surpassed tcsh for 20 years, and bash has surpassed tcsh for 10 years, so you should switch to software maintained this century.
      – Gilles
      May 30 '13 at 22:49










    • Hi Stephane, thanks for a quick answer! The use of csh is not by choice in my case, it is decided by the tools that we use. Can you also suggest a way if the new additions to $PATH are several : delimited directories? Thanks!
      – astralsmith
      May 31 '13 at 7:08










    • @astralsmith, as I said, in (t)csh, the $path array variable is tied to the colon separated scalar variable $PATH. Modifying one will automatically update the other.
      – Stéphane Chazelas
      May 31 '13 at 7:32










    • Thanks Stephane. It works, although the entries are being repeated and not being moved to the front...
      – astralsmith
      May 31 '13 at 7:53














    • 1




      +1 for the "why would you be using csh though?" : please, all csh users, read : faqs.org/faqs/unix-faq/shell/csh-whynot (or the infamous 'Csh-programming considered harmful')
      – Olivier Dulac
      May 30 '13 at 16:31










    • @OlivierDulac That's an argument against csh for scripts, not against csh for interactive use. The real argument against (t)csh for interactive use is that zsh has surpassed tcsh for 20 years, and bash has surpassed tcsh for 10 years, so you should switch to software maintained this century.
      – Gilles
      May 30 '13 at 22:49










    • Hi Stephane, thanks for a quick answer! The use of csh is not by choice in my case, it is decided by the tools that we use. Can you also suggest a way if the new additions to $PATH are several : delimited directories? Thanks!
      – astralsmith
      May 31 '13 at 7:08










    • @astralsmith, as I said, in (t)csh, the $path array variable is tied to the colon separated scalar variable $PATH. Modifying one will automatically update the other.
      – Stéphane Chazelas
      May 31 '13 at 7:32










    • Thanks Stephane. It works, although the entries are being repeated and not being moved to the front...
      – astralsmith
      May 31 '13 at 7:53








    1




    1




    +1 for the "why would you be using csh though?" : please, all csh users, read : faqs.org/faqs/unix-faq/shell/csh-whynot (or the infamous 'Csh-programming considered harmful')
    – Olivier Dulac
    May 30 '13 at 16:31




    +1 for the "why would you be using csh though?" : please, all csh users, read : faqs.org/faqs/unix-faq/shell/csh-whynot (or the infamous 'Csh-programming considered harmful')
    – Olivier Dulac
    May 30 '13 at 16:31












    @OlivierDulac That's an argument against csh for scripts, not against csh for interactive use. The real argument against (t)csh for interactive use is that zsh has surpassed tcsh for 20 years, and bash has surpassed tcsh for 10 years, so you should switch to software maintained this century.
    – Gilles
    May 30 '13 at 22:49




    @OlivierDulac That's an argument against csh for scripts, not against csh for interactive use. The real argument against (t)csh for interactive use is that zsh has surpassed tcsh for 20 years, and bash has surpassed tcsh for 10 years, so you should switch to software maintained this century.
    – Gilles
    May 30 '13 at 22:49












    Hi Stephane, thanks for a quick answer! The use of csh is not by choice in my case, it is decided by the tools that we use. Can you also suggest a way if the new additions to $PATH are several : delimited directories? Thanks!
    – astralsmith
    May 31 '13 at 7:08




    Hi Stephane, thanks for a quick answer! The use of csh is not by choice in my case, it is decided by the tools that we use. Can you also suggest a way if the new additions to $PATH are several : delimited directories? Thanks!
    – astralsmith
    May 31 '13 at 7:08












    @astralsmith, as I said, in (t)csh, the $path array variable is tied to the colon separated scalar variable $PATH. Modifying one will automatically update the other.
    – Stéphane Chazelas
    May 31 '13 at 7:32




    @astralsmith, as I said, in (t)csh, the $path array variable is tied to the colon separated scalar variable $PATH. Modifying one will automatically update the other.
    – Stéphane Chazelas
    May 31 '13 at 7:32












    Thanks Stephane. It works, although the entries are being repeated and not being moved to the front...
    – astralsmith
    May 31 '13 at 7:53




    Thanks Stephane. It works, although the entries are being repeated and not being moved to the front...
    – astralsmith
    May 31 '13 at 7:53












    up vote
    0
    down vote













    I believe that the following can do what you want.



    if ( $PATH =~ */some/path* ) then
    set PATH = ($PATH:/some/path)
    endif


    Note: I'm more of a bash user, so if I have a small error let me know
    csh if test






    share|improve this answer























    • Hi slm, it did not work in my case. I get "Bad : modifier in $ (/).".
      – astralsmith
      May 31 '13 at 11:58















    up vote
    0
    down vote













    I believe that the following can do what you want.



    if ( $PATH =~ */some/path* ) then
    set PATH = ($PATH:/some/path)
    endif


    Note: I'm more of a bash user, so if I have a small error let me know
    csh if test






    share|improve this answer























    • Hi slm, it did not work in my case. I get "Bad : modifier in $ (/).".
      – astralsmith
      May 31 '13 at 11:58













    up vote
    0
    down vote










    up vote
    0
    down vote









    I believe that the following can do what you want.



    if ( $PATH =~ */some/path* ) then
    set PATH = ($PATH:/some/path)
    endif


    Note: I'm more of a bash user, so if I have a small error let me know
    csh if test






    share|improve this answer














    I believe that the following can do what you want.



    if ( $PATH =~ */some/path* ) then
    set PATH = ($PATH:/some/path)
    endif


    Note: I'm more of a bash user, so if I have a small error let me know
    csh if test







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited May 30 '13 at 19:57









    slm

    244k66505669




    244k66505669










    answered May 30 '13 at 14:51









    demure

    436410




    436410












    • Hi slm, it did not work in my case. I get "Bad : modifier in $ (/).".
      – astralsmith
      May 31 '13 at 11:58


















    • Hi slm, it did not work in my case. I get "Bad : modifier in $ (/).".
      – astralsmith
      May 31 '13 at 11:58
















    Hi slm, it did not work in my case. I get "Bad : modifier in $ (/).".
    – astralsmith
    May 31 '13 at 11:58




    Hi slm, it did not work in my case. I get "Bad : modifier in $ (/).".
    – astralsmith
    May 31 '13 at 11:58










    up vote
    0
    down vote













    (note: i can not test the below right now... hope it works)



    I just add this as a complement to stephane answer: how to get rid of duplicates in $PATH



    I'll assume you have




    • directories that should stay in front

    • and other directories as well, order not important (ie, placed AFTER the above)


    so:



    To un-duplicate entries:



    UNIQUE_LIST=$( echo "$PATH" | tr ':' 'n' | sort | uniq)

    # then we place in front those from UNIQUELIST that match an ordered list
    # note that that way, those who didn't have "/sbin" still won't have it, but if they did
    # it will be at the right place in the list
    shouldbefirst="/bin /sbin /usr/bin" # complete or re-order as needed on your system...
    for dir in $shouldbefirst
    do
    if ( echo "$UNIQUE_LIST" | grep "$dir" >/dev/null 2>/dev/null)
    then #we have this dir in UNIQUE_LIST
    NEWLIST="${NEWLIST}:${dir}"
    UNIQUE_LIST="$( echo "$UNIQUE_LIST" | grep -v "^$dir$")" #we treated that one, take it out of the original list
    fi
    done

    # then put the remaining of UNIQUE_LIST in the order you want (here, alphabetically)
    for dir in $UNIQUE_LIST
    do
    NEWLIST="${NEWLIST}:${dir}"
    UNIQUE_LIST="$( echo "$UNIQUE_LIST" | grep -v "^$dir$")" #we treated that one, take it out of the original list
    done

    # get rid of possible first ":" (as NEWLIST starts empty)
    NEWLIST="$(echo "$NEWLIST" | sed -e 's/^://')"

    # and then : (I test by placing "echo" in front, get rid of "echo" if it looks fine)
    echo PATH="$NEWLIST"


    (once again, i can not test right now, but hope it helps)



    NOTE: I'll add recommendation: get rid of "." in your PATH, as it will be "bumped" up to just after the SHOULDBEFIRST dirs ... ("." should always be avoided, and if used, always in the last place only, so that you can't easily bypass commands from /bin, /usr/bin, etc)






    share|improve this answer

























      up vote
      0
      down vote













      (note: i can not test the below right now... hope it works)



      I just add this as a complement to stephane answer: how to get rid of duplicates in $PATH



      I'll assume you have




      • directories that should stay in front

      • and other directories as well, order not important (ie, placed AFTER the above)


      so:



      To un-duplicate entries:



      UNIQUE_LIST=$( echo "$PATH" | tr ':' 'n' | sort | uniq)

      # then we place in front those from UNIQUELIST that match an ordered list
      # note that that way, those who didn't have "/sbin" still won't have it, but if they did
      # it will be at the right place in the list
      shouldbefirst="/bin /sbin /usr/bin" # complete or re-order as needed on your system...
      for dir in $shouldbefirst
      do
      if ( echo "$UNIQUE_LIST" | grep "$dir" >/dev/null 2>/dev/null)
      then #we have this dir in UNIQUE_LIST
      NEWLIST="${NEWLIST}:${dir}"
      UNIQUE_LIST="$( echo "$UNIQUE_LIST" | grep -v "^$dir$")" #we treated that one, take it out of the original list
      fi
      done

      # then put the remaining of UNIQUE_LIST in the order you want (here, alphabetically)
      for dir in $UNIQUE_LIST
      do
      NEWLIST="${NEWLIST}:${dir}"
      UNIQUE_LIST="$( echo "$UNIQUE_LIST" | grep -v "^$dir$")" #we treated that one, take it out of the original list
      done

      # get rid of possible first ":" (as NEWLIST starts empty)
      NEWLIST="$(echo "$NEWLIST" | sed -e 's/^://')"

      # and then : (I test by placing "echo" in front, get rid of "echo" if it looks fine)
      echo PATH="$NEWLIST"


      (once again, i can not test right now, but hope it helps)



      NOTE: I'll add recommendation: get rid of "." in your PATH, as it will be "bumped" up to just after the SHOULDBEFIRST dirs ... ("." should always be avoided, and if used, always in the last place only, so that you can't easily bypass commands from /bin, /usr/bin, etc)






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        (note: i can not test the below right now... hope it works)



        I just add this as a complement to stephane answer: how to get rid of duplicates in $PATH



        I'll assume you have




        • directories that should stay in front

        • and other directories as well, order not important (ie, placed AFTER the above)


        so:



        To un-duplicate entries:



        UNIQUE_LIST=$( echo "$PATH" | tr ':' 'n' | sort | uniq)

        # then we place in front those from UNIQUELIST that match an ordered list
        # note that that way, those who didn't have "/sbin" still won't have it, but if they did
        # it will be at the right place in the list
        shouldbefirst="/bin /sbin /usr/bin" # complete or re-order as needed on your system...
        for dir in $shouldbefirst
        do
        if ( echo "$UNIQUE_LIST" | grep "$dir" >/dev/null 2>/dev/null)
        then #we have this dir in UNIQUE_LIST
        NEWLIST="${NEWLIST}:${dir}"
        UNIQUE_LIST="$( echo "$UNIQUE_LIST" | grep -v "^$dir$")" #we treated that one, take it out of the original list
        fi
        done

        # then put the remaining of UNIQUE_LIST in the order you want (here, alphabetically)
        for dir in $UNIQUE_LIST
        do
        NEWLIST="${NEWLIST}:${dir}"
        UNIQUE_LIST="$( echo "$UNIQUE_LIST" | grep -v "^$dir$")" #we treated that one, take it out of the original list
        done

        # get rid of possible first ":" (as NEWLIST starts empty)
        NEWLIST="$(echo "$NEWLIST" | sed -e 's/^://')"

        # and then : (I test by placing "echo" in front, get rid of "echo" if it looks fine)
        echo PATH="$NEWLIST"


        (once again, i can not test right now, but hope it helps)



        NOTE: I'll add recommendation: get rid of "." in your PATH, as it will be "bumped" up to just after the SHOULDBEFIRST dirs ... ("." should always be avoided, and if used, always in the last place only, so that you can't easily bypass commands from /bin, /usr/bin, etc)






        share|improve this answer












        (note: i can not test the below right now... hope it works)



        I just add this as a complement to stephane answer: how to get rid of duplicates in $PATH



        I'll assume you have




        • directories that should stay in front

        • and other directories as well, order not important (ie, placed AFTER the above)


        so:



        To un-duplicate entries:



        UNIQUE_LIST=$( echo "$PATH" | tr ':' 'n' | sort | uniq)

        # then we place in front those from UNIQUELIST that match an ordered list
        # note that that way, those who didn't have "/sbin" still won't have it, but if they did
        # it will be at the right place in the list
        shouldbefirst="/bin /sbin /usr/bin" # complete or re-order as needed on your system...
        for dir in $shouldbefirst
        do
        if ( echo "$UNIQUE_LIST" | grep "$dir" >/dev/null 2>/dev/null)
        then #we have this dir in UNIQUE_LIST
        NEWLIST="${NEWLIST}:${dir}"
        UNIQUE_LIST="$( echo "$UNIQUE_LIST" | grep -v "^$dir$")" #we treated that one, take it out of the original list
        fi
        done

        # then put the remaining of UNIQUE_LIST in the order you want (here, alphabetically)
        for dir in $UNIQUE_LIST
        do
        NEWLIST="${NEWLIST}:${dir}"
        UNIQUE_LIST="$( echo "$UNIQUE_LIST" | grep -v "^$dir$")" #we treated that one, take it out of the original list
        done

        # get rid of possible first ":" (as NEWLIST starts empty)
        NEWLIST="$(echo "$NEWLIST" | sed -e 's/^://')"

        # and then : (I test by placing "echo" in front, get rid of "echo" if it looks fine)
        echo PATH="$NEWLIST"


        (once again, i can not test right now, but hope it helps)



        NOTE: I'll add recommendation: get rid of "." in your PATH, as it will be "bumped" up to just after the SHOULDBEFIRST dirs ... ("." should always be avoided, and if used, always in the last place only, so that you can't easily bypass commands from /bin, /usr/bin, etc)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jun 5 '13 at 15:22









        Olivier Dulac

        3,8051324




        3,8051324






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f77661%2fpreventing-duplicate-entried-in-path-cshrc%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            サソリ

            広島県道265号伴広島線

            Accessing regular linux commands in Huawei's Dopra Linux