Add directory to $PATH if it's not already there

Posted by Doug Harris on Super User See other posts from Super User or by Doug Harris
Published on 2009-09-11T16:19:11Z Indexed on 2012/06/29 3:19 UTC
Read the original article Hit count: 245

Filed under:
|
|

Has anybody written a bash function to add a directory to $PATH only if it's not already there?

I typically add to PATH using something like:

export PATH=/usr/local/mysql/bin:$PATH

If I construct my PATH in .bash_profile, then it's not read unless the session I'm in is a login session -- which isn't always true. If I construct my PATH in .bashrc, then it runs with each subshell. So if I launch a Terminal window and then run screen and then run a shell script, I get:

$ echo $PATH
/usr/local/mysql/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:....

I'm going to try building a bash function called add_to_path() which only adds the directory if it's not there. But, if anybody has already written (or found) such a thing, I won't spend the time on it.

© Super User or respective owner

Related posts about bash

Related posts about script