Created
October 16, 2014 12:29
-
-
Save JohnArchieMckown/dfb8220faa8e45a167dc to your computer and use it in GitHub Desktop.
pathmunge is a shell function to add a directory to the PATH. It will add the specified directory only if: (1) it currently exists and (2) it is not already on the PATH. It can add the directory either at the beginning of the PATH (defautl) or at the end of the PATH.
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
# System wide environment and startup programs, for login setup | |
# Functions and aliases go in /etc/bashrc | |
pathmunge () { | |
if [ -e $1 ]; then | |
if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then | |
if [ "$2" = "after" ] ; then | |
PATH=$PATH:$1 | |
else | |
PATH=$1:$PATH | |
fi | |
fi | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For others seeing this, i checked with
zprof
and the grep call on line 5 causes pathmunge to take a very long time on shell startup if you're calling it multiple times (12 calls for me was ~1sec). Not sure of all the edge cases but I replaced it with a fasterif ! [[ "$PATH" = *"$1"* ]]; then