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.
path csh
add a comment |
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.
path csh
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
add a comment |
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.
path csh
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
path csh
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
add a comment |
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
add a comment |
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
)
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
|
show 7 more comments
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
Hi slm, it did not work in my case. I get "Bad : modifier in $ (/).".
– astralsmith
May 31 '13 at 11:58
add a comment |
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)
add a comment |
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
)
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
|
show 7 more comments
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
)
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
|
show 7 more comments
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
)
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
)
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
|
show 7 more comments
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
|
show 7 more comments
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
Hi slm, it did not work in my case. I get "Bad : modifier in $ (/).".
– astralsmith
May 31 '13 at 11:58
add a comment |
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
Hi slm, it did not work in my case. I get "Bad : modifier in $ (/).".
– astralsmith
May 31 '13 at 11:58
add a comment |
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
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
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
add a comment |
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
add a comment |
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)
add a comment |
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)
add a comment |
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)
(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)
answered Jun 5 '13 at 15:22
Olivier Dulac
3,8051324
3,8051324
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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