Skip to content

Instantly share code, notes, and snippets.

@7cc
Last active February 20, 2020 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 7cc/84dc9be834b8924a65e5cf02e54b02a0 to your computer and use it in GitHub Desktop.
Save 7cc/84dc9be834b8924a65e5cf02e54b02a0 to your computer and use it in GitHub Desktop.
remove a path from the $PATH in Bash
# this will leave trailing :
removeFromPath() {
PATH=$(echo -n $PATH | tr ":" "\n" | fgrep -vx "$1" | tr "\n" ":")
}
removeFromPath() {
local NEWPATH
NEWPATH=$(echo "${PATH//:/$'\n'}" | fgrep -vx "/usr/bin")
NEWPATH=$(echo "${NEWPATH//$'\n'/:}")
PATH=$NEWPATH
}
removeFromPath() {
local REMDIR="$1"
local NEWPATH=$(echo :$PATH: | perl -pe "
s|:\\Q${REMDIR}:||g;
s/^:|:$//g;
")
echo $NEWPATH
}
# does not work with *
removeFromPath() {
# https://stackoverflow.com/questions/370047/what-is-the-most-elegant-way-to-remove-a-path-from-the-path-variable-in-bash#2108540
local REMOVE WORK
REMOVE=":$1:"
WORK=":$PATH:"
WORK=${WORK//$REMOVE/:}
WORK=${WORK/#:/}
WORK=${WORK/%:/}
PATH=$WORK
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment