Remove a path from the PATH environment variable
suggest changeTo remove a PATH from a PATH environment variable, you need to edit ~/.bashrc or ~/.bash_profile or /etc/profile or ~/.profile or /etc/bash.bashrc (distro specific) file and remove the assignment for that particular path.
Instead of finding the exact assignment, you could just do a replacement in the $PATH
in its final stage.
The following will safely remove $path
from $PATH
:
path=~/bin
PATH="$(echo "$PATH" |sed -e "s#\(^\|:\)$(echo "$path" |sed -e 's/[^^]/[&]/g' -e 's/\^/\\^/g')\(:\|/\{0,1\}$\)#\1\2#" -e 's#:\+#:#g' -e 's#^:\|:$##g')"
To make it permanent, you will need to add it at the end of your bash configuration file.
You can do it in a functional way:
rpath(){
for path in "$@";do
PATH="$(echo "$PATH" |sed -e "s#\(^\|:\)$(echo "$path" |sed -e 's/[^^]/[&]/g' -e 's/\^/\\^/g')\(:\|/\{0,1\}$\)#\1\2#" -e 's#:\+#:#g' -e 's#^:\|:$##g')"
done
echo "$PATH"
}
PATH="$(rpath ~/bin /usr/local/sbin /usr/local/bin)"
PATH="$(rpath /usr/games)"
# etc ...
This will make it easier to handle multiple paths.
Notes:
- You will need to add these codes in the Bash configuration file (~/.bashrc or whatever).
- Run
source ~/.bashrc
to reload the Bash configuration (~/.bashrc) file.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents