Created
October 10, 2012 14:32
-
-
Save antmd/3866000 to your computer and use it in GitHub Desktop.
pathmunge
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 -- 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