Thursday Sep 06, 2007

PATH redundancies

How many people out there have huge PATHs partly because your work requires various tools from different installations, and partly because you end up with multiple instances of the same directory within your PATH?

I suspect more than a few. I just recently added this to the end of my .bashrc, but I welcome more efficient ways of accomplishing the same thing:

PATH="$(echo "$PATH" | tr : '\\012' |
      perl -n -e 'chomp; if (!defined($h{$_})) { print ":" if $. > 1; print "$_"; $h{$_} = 1; }')"

Invoking perl every time I start a shell is, shall we say, less than satisfying.

Update: From Nevin comes this bash function, which couldn't be formatted properly in the comments section, so I add it here between a couple of pretty <pre> tags:

strippath()
{
   local ifs="$IFS"
   IFS=:

   local path
   path=(${1:-${PATH}})

   local p=${#path[\*]}
   until ((0 == p))
   do
      ((--p))
      if [ -d ${path[$p]} ]
      then
         local e;
         for ((e = 0; e != p; ++e))
         do
            if [ "${path[$p]}" == "${path[$e]}" ]
            then
               unset path[$p]
               break;
            fi
         done
      else
         unset path[$p]
      fi
   done

   echo "${path[\*]}"
   IFS="$ifs"
}

PATH="$(strippath)"
About

Known throughout Sun as a man of infinite wit, of jovial attitude, and of making things up about himself at the slightest whim.

Search

Archives
« July 2014
MonTueWedThuFriSatSun
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
   
       
Today