Last active
February 20, 2020 03:21
-
-
Save 7cc/84dc9be834b8924a65e5cf02e54b02a0 to your computer and use it in GitHub Desktop.
remove a path from the $PATH in Bash
This file contains hidden or 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
# this will leave trailing : | |
removeFromPath() { | |
PATH=$(echo -n $PATH | tr ":" "\n" | fgrep -vx "$1" | tr "\n" ":") | |
} |
This file contains hidden or 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
removeFromPath() { | |
local NEWPATH | |
NEWPATH=$(echo "${PATH//:/$'\n'}" | fgrep -vx "/usr/bin") | |
NEWPATH=$(echo "${NEWPATH//$'\n'/:}") | |
PATH=$NEWPATH | |
} |
This file contains hidden or 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
removeFromPath() { | |
local REMDIR="$1" | |
local NEWPATH=$(echo :$PATH: | perl -pe " | |
s|:\\Q${REMDIR}:||g; | |
s/^:|:$//g; | |
") | |
echo $NEWPATH | |
} |
This file contains hidden or 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
# 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