Skip to content

Instantly share code, notes, and snippets.

@JohnArchieMckown
Created October 16, 2014 12:29
Show Gist options
  • Save JohnArchieMckown/dfb8220faa8e45a167dc to your computer and use it in GitHub Desktop.
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.
# 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
}
@Amar1729
Copy link

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 faster if ! [[ "$PATH" = *"$1"* ]]; then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment