Path modifier script

I just discovered that XQuartz on Mountain Lion does some annoying things to my PATH, changing:


in a Terminal window to


in an xterm.

The duplicate entries aren’t so bad, but moving the system directories in front of Fink’s is annoying.  Running again doesn’t change this, unfortunately.  I came up with a script which lets me reorder the PATH by blocks and remove via a pattern match any items I don’t want.  As an afterthought, it removes duplicates, too.  By running

export PATH=` --delete=X11R6`

I am able to restore the PATH settings to the xterm that I have in my Terminal window. The script follows:

#!/usr/bin/env perl -w

# Read in the PATH environment variable and
# 1) Strip out duplicate entries
# 2) Delete entries matching a pattern given by instances of the --delete flag
# 3) Put the entries in the order Fink, system, user.
# This could certainly be extended.

use strict;
use List::MoreUtils qw { uniq };
use Getopt::Long;

my @delete_vals;
my $result=GetOptions ("delete=s" => \@delete_vals);

my (@finkpath, @userpath, @generalpath);

ENTRY: foreach my $entry (uniq (split /:/, $ENV{"PATH"})) {
     foreach (@delete_vals) {
          next ENTRY if $entry =~ /$_/;
     if ($entry =~ m|^/sw|) {
          push @finkpath, ($entry);
     } elsif ($entry =~ m/$ENV{"HOME"}/) {
          push @userpath, ($entry);
     } else {
          push @generalpath, ($entry);
print join(':', @finkpath, @generalpath, @userpath),"\n";

Tags: , ,

4 Responses to “Path modifier script”

  1. finkakh Says:

    Reblogged this on Finkers: a blog about Fink.

  2. finkakh Says:

    The following is convenient:

    1) I created a script called “” in /sw/etc/profile.d with contents:

    #!/bin/sh -ev
    export PATH=`/Users/finkakh/bin/`

    . The name isn’t important, because fink’s environment initialization ( or init.csh) sources everything in that directory.

    2) I set the Terminal item in the X11 Applications menu to be “xterm -ls”, i.e. ‘login shell’, so that and profile.d/ get processed. (The default on Xquartz is now to start with whatever the user’s PATH setting is, and older X11 releases just ignored it–this gives consistent results for both cases)

  3. finkakh Says:

    I’ve made up a Fink package for this called “fixpath”–I renamed the Perl script to “”. There’s a github repository for it:

  4. fixpath-1.0.0 « akh's Fink Blog Says:

    […] all supported OS X.  This contains the path-fixing script that I mentioned […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: