Last active
November 22, 2015 10:32
-
-
Save dyoung522/349046b99aaf65c1c80e to your computer and use it in GitHub Desktop.
pathmunge() bash function to add to PATH without duplication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pathmunge () { | |
if [ "$2" = "force" ] || ! echo $PATH | $(which egrep) -q "(^|:)$1($|:)" | |
then | |
if [ "$2" = "after" ] | |
then | |
PATH=$PATH:$1 | |
else | |
PATH=$1:$PATH | |
fi | |
export PATH | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's no quote for the PATH assignments, then if there's space or something alike in "$1" or "$PATH", then the function behaviour is undefined.
And also, why bother using which and egrep, they are external commands and just too slow.