Fri, 09 Feb 2007

Removing duplicate PATH entries


I was creating a little setup shell script to be sourced to set up an environment. It's the standard thing; set a few environment variables, play with the filesystem, start some things. Part of it adds directories to PATH and LD_LIBRARY_PATH.

Then I wondered about it being run more than once. I don't imagine great problems, but multiple entries in the PATH look ugly.

So I wrote this little function to clean up the PATH, or any other similar variable:

tidy () { echo -n $1 | perl -naF: -e 'print join ":", grep !$s{$_}++, @F' }

It can be called as:

export PATH=$(tidy $sw/bin:$PATH)
export LD_LIBRARY_PATH=$(tidy $LD_LIBRARY_PATH:$sw/lib:$ORACLE_HOME/lib)

which seems to work well enough.

[/unix] permanent link




November 2022
Sun Mon Tue Wed Thu Fri Sat