Skip to content

Instantly share code, notes, and snippets.

@eshch
Last active February 22, 2018 19:55
Show Gist options
  • Save eshch/1ee76774c407a78c955ba18ab2d1ae96 to your computer and use it in GitHub Desktop.
Save eshch/1ee76774c407a78c955ba18ab2d1ae96 to your computer and use it in GitHub Desktop.
Remove path from PATH environment variable
# Don't consider this snippet as helpful. Just some excersize with shell.
# Usually you do not need to remove a path from PATH because you are able not to add
# the the path to it.
# But once you want to move a path from the middle of PATH to head, so you should
# delete it first if you are kind of a perfectionist. :(
# HATE BASH!
# Remove element in joined array like PATH standard environment variable.
del-element()
{
awk -v sep="$1" -v path="$2" '
BEGIN {
FS = sep
}
{
start = 1
for (i = 1; i <= NF; ++i) {
if ($i == path)
continue
if (!start)
printf(sep)
printf("%s", $i)
start = 0
}
}'
}
pathdel()
{
PATH=$(echo $PATH | del-element : "$1")
}
pathmovetohead()
{
pathdel "$1"
pathaddhead "$1"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment