This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Useful Cygwinism #2: PATH Despacifier/Deduplicateifier


I've been noticing a lot of traffic lately on people having problems with paths
which contain spaces, admonitions to "keep your PATH as short as possible for
performance reasons", etc.  So in the spirit of floating everyone's boat, here's
installment #2 of "Useful Cygwinisms".

PROBLEM:
Your Windows PATH contains paths with spaces in them, causing you all manner of
grief.  It also contains the Windows paths to /bin and /usr/local/bin, to make
running Cygwin apps from the 9x/NT command line easier, but these end up getting
needlessly duplicated when you run a bash session, due to the actions of
/etc/profile.

SOLUTION:
A little Perl script I like to call "despaceify":

=========================================================
#!/usr/bin/perl -w
use strict;
my @pathcomps;
my @pathcomps_out;
my %comps_seen_already;
@pathcomps = split(/:/, shift);
for(@pathcomps)
{
        if(!exists($comps_seen_already{$_}))
        {
                my $path;
                $path='"'.$_.'"';
                $path=qx(cygpath -u \$(cygpath -ws $path));
                $path=~tr/\n//d;
                push(@pathcomps_out, $path);
        }
        $comps_seen_already{$_}=1;
}
print join(":", @pathcomps_out);
==========================================================

This script takes a ":"-separated Unixoid PATH, replaces any component paths
containing spaces with their equivalent "8.3" spaceless replacements, and also
removes any duplicates (only the first instance of a particular path will
remain).  To make it work its magic, just save the script as "despaceify"
somewhere convenient (for me, ~/bin/despaceify), of course make sure you have
Perl installed, and then add this to your .bash_profile:

# Clean up PATH.
export PATH=$(~/bin/despaceify "$PATH");

And that's it.  Every time you start up a bash session, your PATH will be
automatically scrubbed clean!

COMMENTS:
This addition adds a slightly annoying ~1-2 second delay to bash startup,
probably from the loading of Perl.  However, my Herculean efforts to get such
functionality without Perl (i.e. with just shell scripting and cygpath) came to
naught.  I'm guessing an equivalent script might be possible in awk, but that's
an exercise (==challenge) for the reader ;-).

FINE PRINT:
Offer void in CA, AK, WA, and where prohibited by law.  No bailment created.
Not to be used as a flotation device.  Phenylketonurics: Contains phenylalanine.
To be used only for good, never evil.  Contains less than 10% fruit juice.  May
not be suitable for some viewers.  Light fuse and get away.

--
Gary R. Van Sickle
Brewer.  Patriot.


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]