Home » Questions » Computers [ Ask a new question ]

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

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

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

Asked by: Guest | Views: 300
Total answers/comments: 5
Guest [Entry]

"From my .bashrc:

pathadd() {
if [ -d ""$1"" ] && [[ "":$PATH:"" != *"":$1:""* ]]; then
PATH=""${PATH:+""$PATH:""}$1""
fi
}

Note that PATH should already be marked as exported, so reexporting is not needed. This checks whether the directory exists & is a directory before adding it, which you may not care about.

Also, this adds the new directory to the end of the path; to put at the beginning, use PATH=""$1${PATH:+"":$PATH""}"" instead of the above PATH= line."
Guest [Entry]

"Here's something from my answer to this question combined with the structure of Doug Harris' function. It uses Bash regular expressions:

add_to_path ()
{
if [[ ""$PATH"" =~ (^|:)""${1}""(:|$) ]]
then
return 0
fi
export PATH=${1}:$PATH
}"
Guest [Entry]

"Here's an alternative solution that has the additional advantage of removing redundant entires:

function pathadd {
PATH=:$PATH
PATH=$1${PATH//:$1/}
}

The single argument to this function is prepended to the PATH, and the first instance of the same string is removed from the existing path. In other words, if the directory already exists in the path, it is promoted to the front rather than added as a duplicate.

The function works by prepending a colon to the path to ensure that all entries have a colon at the front, and then prepending the new entry to the existing path with that entry removed. The last part is performed using bash's ${var//pattern/sub} notation; see the bash manual for more details."
Guest [Entry]

"Here's mine (I believe it was written years ago by Oscar, the sysadmin of my old lab, all credit to him), its been around in my bashrc for ages. It has the added benefit of allowing you to prepend or append the new directory as desired:

pathmunge () {
if ! echo $PATH | /bin/egrep -q ""(^|:)$1($|:)"" ; then
if [ ""$2"" = ""after"" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
fi
}

Usage:

$ echo $PATH
/bin/:/usr/local/bin/:/usr/bin
$ pathmunge /bin/
$ echo $PATH
/bin/:/usr/local/bin/:/usr/bin
$ pathmunge /sbin/ after
$ echo $PATH
/bin/:/usr/local/bin/:/usr/bin:/sbin/"
Guest [Entry]

"For prepending, I like @Russell's solution, but there's a small bug: if you try to prepend something like ""/bin"" to a path of ""/sbin:/usr/bin:/var/usr/bin:/usr/local/bin:/usr/sbin"" it replaces ""/bin:"" 3 times (when it didn't really match at all). Combining a fix for that with the appending solution from @gordon-davisson, I get this:

path_prepend() {
if [ -d ""$1"" ]; then
PATH=${PATH//"":$1:""/:} #delete all instances in the middle
PATH=${PATH/%"":$1""/} #delete any instance at the end
PATH=${PATH/#""$1:""/} #delete any instance at the beginning
PATH=""$1${PATH:+"":$PATH""}"" #prepend $1 or if $PATH is empty set to $1
fi
}"