Skip to content

Instantly share code, notes, and snippets.

@antmd
Created October 10, 2012 14:32
Show Gist options
  • Save antmd/3866000 to your computer and use it in GitHub Desktop.
Save antmd/3866000 to your computer and use it in GitHub Desktop.
pathmunge
# Pathmunge -- add a new path to a path variable
# arg1 - name of path variable
# arg2 - new path to add
# arg3 - if present APPEND rather than PREPEND
pathmunge () {
local pathVar=$1
local pathVal=$(eval echo \$$pathVar)
local newVal=$2
local after=$3
if [ -z "$pathVal" ]; then
pathVal=$newVal
elif ! echo $pathVal | /usr/bin/egrep -q "(^|:)$newVal($|:)" >/dev/null 2>&1 ; then
if [ ! -z "$after" ] ; then
pathVal=$pathVal:$newVal
else
pathVal=$newVal:$pathVal
fi
fi
eval $pathVar=$pathVal
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment