I have the following code that's source-d by my .shellrcPATH=${PATH}:${HOME}/perl5/binPATH=${PATH}:${HOME}/.binexport PATHbut 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?
keep duplicates out of $PATH on source
shell;shell script;environment variables;path;posix
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;;    esac  done}add_to_PATH ~/perl5/bin ~/.binThe 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 toif ! [ -d $d ]; then continue; fiNote 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.