Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I have the following code that's source-d by my .shellrc

export PATH

but if I make changes to other code and then source this file, my path continues to get longer and longer with each source, each time appending these when they're already there. What can I do to prevent this?

share|improve this question

marked as duplicate by goldilocks, Drav Sloan, jimmij, Braiam, Ramesh Aug 10 '15 at 16:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

See also: How do I manipulate $PATH elements in shell scripts? on stack overflow. – dmckee Dec 22 '10 at 15:01
up vote 8 down vote accepted
add_to_PATH () {
  for d; do
    d=$(cd -- "$d" && { pwd -P || pwd; }) 2>/dev/null  # canonicalize symbolic links
    if [ -z "$d" ]; then continue; fi  # skip nonexistent directory
    case ":$PATH:" in
      *":$d:"*) :;;
      *) PATH=$PATH:$d;;
add_to_PATH ~/perl5/bin ~/.bin

The line for symbolic link canonicalization is optional. If you remove it, also remove the next line (if you want to keep nonexistent directories), or change it to

if ! [ -d "$d" ]; then continue; fi

Note that the symlink canonicalization method only guarantees unicity amongst directories that were added by this function. It also doesn't handle edge cases like an NFS directory mounted on two locations or a Linux bind mount.

share|improve this answer
This is close to a shell agnostic idempotent append, but even this snippet will yield ~/perl5/bin:~/.bin:~/.bin when run twice because of the lack of trailing : from the first run. And you can't add a trailing : because the empty component thus generated implies .. – msw Dec 18 '10 at 14:43
@msw: No, this snippet will yield /usr/local/bin:/usr/bin:/bin:/home/xenoterracide/perl5/bin:/home/xenoterracide/‌​.bin if run twice (assuming PATH is /usr/local/bin:/usr/bin:/bin initially). Note that I'm matching :$PATH:, not $PATH. – Gilles Dec 18 '10 at 15:20
I did indeed miss the additional colons; you are correct. +1 – msw Dec 18 '10 at 15:45
since I didn't expect to get a loop... if I'm only doing path at a time... ~/perl5 doesn't exist on all systems... so what's the best way to do this if I'm only doing one at a time? either that or what's the best way to have this code test for directory existence before adding them to the path – xenoterracide Dec 18 '10 at 22:24

I use these functions that are sourced from an initialization script by fink on os x (so credit goes to the fink developers). They work great and I can re-source my .bash_profile whenever I want. Don't ask me how they work... I just know they do :)

# define append_path and prepend_path to add directory paths, e.g. PATH, MANPATH
# add to end of path
  if ! eval test -z "\"\${$1##*:$2:*}\"" -o -z "\"\${$1%%*:$2}\"" -o -z "\"\${$1##$2:*}\"" -o -z "\"\${$1##$2}\"" ; then
    eval "$1=\$$1:$2"

# add to front of path
  if ! eval test -z "\"\${$1##*:$2:*}\"" -o -z "\"\${$1%%*:$2}\"" -o -z "\"\${$1##$2:*}\"" -o -z "\"\${$1##$2}\"" ; then
    eval "$1=$2:\$$1"

I can use them like so to append or prepend to $PATH or $MANPATH (they'll work with any variable formatted like $PATH):

prepend_path PATH $macPortsDir/sbin
prepend_path MANPATH $macPortsDir/man
share|improve this answer

One thing you could do is use an environment variable as a guard. So set the env to __<your script>__path_added.

In your script, you can then just test if that has been set before adding the path in. A bit like you would a C header guard.

share|improve this answer

You could put a test around the "append this directory to path" command which would check to see if foo is already in the path before adding it, but it wouldn't buy you much.

First, the test itself would be costly compared to appending a duplicate element. Secondly, a redundant element later in the path has no effect upon what does get executed when you execute a given command because the first matching executable in the path will still be the one executed. Finally most shells cache prior path hits in a hash table so the second time you execute my_command the path isn't even searched.

About the only thing that not appending redundant entries will get you is a prettier looking path, but most paths are pretty ugly to begin with. If this aesthetic goal is really important to you, tell us which shell you are using and I can conjure up a function to "append this to path only if it isn't present" function.

share|improve this answer
would it really end up being shell specific? :( I'm trying to keep this file shell agnostic – xenoterracide Dec 18 '10 at 13:14
A redundant path does mean slower traversals sometimes. You can feel it when you have path directories on NFS and you mistype a command name or your shell's cache isn't aggressive enough. – Gilles Dec 18 '10 at 14:34
@Gilles, agreed. But, as noted in my comment to your answer, the semantics of null PATH components make this problem harder than it appears at first. – msw Dec 18 '10 at 14:45
Also, twiddle ~ expansion and symlinks make this problem nearly insoluble in the general case. – msw Dec 18 '10 at 14:51
~ expansion has nothing to do with this. If you want to eliminate a component that's a symlink to another component, that's a different problem, but it's solvable (with the caveat that symlinks can change over time). – Gilles Dec 18 '10 at 15:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.