I have been using the following (Bourne / Korn / POSIX / Bash) script for most of the decade:
: "@(#)$Id: clnpath.sh,v 1.6 1999/06/08 23:34:07 jleffler Exp $" # # Print minimal version of $PATH, possibly removing some items case $# in 0) chop=""; path=${PATH:?};; 1) chop=""; path=$1;; 2) chop=$2; path=$1;; *) echo "Usage: `basename $0 .sh` [$PATH [remove:list]]" >&2 exit 1;; esac # Beware of the quotes in the assignment to chop! echo "$path" | ${AWK:-awk} -F: '# BEGIN { # Sort out which path components to omit chop="'"$chop"'"; if (chop != "") nr = split(chop, remove); else nr = 0; for (i = 1; i <= nr; i++) omit[remove[i]] = 1; } { for (i = 1; i <= NF; i++) { x=$i; if (x == "") x = "."; if (omit[x] == 0 && path[x]++ == 0) { output = output pad x; pad = ":"; } } print output; }'
In the Korn shell, I use:
export PATH=$(clnpath /new/bin:/other/bin:$PATH /old/bin:/extra/bin)
This leaves me with a PATH containing the new and other bin directories in front, plus one copy of each directory name in the main path value, except that the old and additional bin directories are deleted by bin.
You will need to adapt this to the C shell (sorry - but I really believe in the truths proclaimed in C Shell Programming is considered harmful ). First of all, you donβt have to bother with a colon separator, so life is actually easier.
Jonathan Leffler Sep 26 '08 at 6:03 2008-09-26 06:03
source share