The Shining $PATH

March 21st, 2008

The search-path algorithm for UNIX is broken, and has been broken for a long time.

$PATH is fine for specifying bin directories; in fact, there ought to be just one $PATH per host, which all processes can inherit, rather than users having to set such things in their dotfiles. $PATH isn’t obsolete, but it needs help.

The problem is with interpreting the order of the PATH variable. The historic algorithm, implemented by the first exec() and its descendants, is to search each of the dirs in $PATH, in the order in which they appear, until a file matching the provided command name is found, and then exec that file. It’s a simple algorithm: fast, easy to implement … but it’s also the cause of a whole genre of UNIX aggravations.

Now that we live in The Future, and our computers are smart, here’s my vote for a better algorithm: first match the command name against pattern keys in the list of key-value pairs in the $PATH_HINTS variable; if nothing is found, default to standard UNIX behaviour.

The PATH_HINTS variable would map patterns to bin directories, like so:

mysql.*=/usr/local/mysql/bin;php$=/usr/bin;php4=/usr/old/bin

This expresses something like: if i’m executing “mysql” or “mysqldump” or “mysqladmin”, look for it in /usr/local/mysql/bin. If i’m executing “php”, look in /usr/bin — but if i’m “php4”, run it from /usr/old/bin; for “php5” or “php6” or “php2000”, follow the search path like you usually would.

I’m using regular expressions here; someone might be able to convince me that file-glob matching patterns are better, perhaps for performance reasons … but if you’re really concerned about exec performance, provide a complete path to the file you want to execute. Duh.

My system has three copies of mysql installed, and I need the second one … which lives in /Applications/MAMP/Library/bin, if you can believe it. (UNIX veterans are cringing, of course.) I don’t want to re-order my PATH just for that, because re-ordering my PATH could have unintended consequences, especially as regards the three versions of Perl on my system. (And yes, I have my reasons, and they’re good reasons.)

One Response to “The Shining $PATH”

  1. ben.foote@pdxstump.com Says:

    $PATH is manipulated system wide by /etc/profile /etc/bashrc and their ilk

    why not create a directory to hold symbolic links to your desired binaries and then put that directory at the top of your PATH

    if you’d really like to use regex’s then write a script to setup the links in the directory

    This should give you what you’d like while maintaining the traditional expectations of $PATH

Leave a Reply