scatterd-dotfiles/zsh/functions/move_to_front_of_path
David Alexander f7c73f7c1b Deduplicate PATH before exporting
Why:

* Remove dupes from PATH

How:

$PATH is a scalar composed of contents of $path, so it uses typeset -U
("U" as in Unique) to ensure there are no dupes. It preserves the first
occurrence in the array, since it would be searched first before others
when matching a command in the directories in the PATH anyway.
2015-11-13 15:25:55 -05:00

10 lines
287 B
Text

# Reorders existing entry in PATH to have it at the front
#
# move_to_front_of_path "$HOME/.bin"
#
move_to_front_of_path() {
if [[ ":$PATH:" == *":$1:"* ]]; then
PATH=$(echo $PATH | sed 's#'$1'##g' | sed s/:://g | sed s/:$//g | sed s/^://g)
PATH="$1${PATH:+":$PATH"}"
fi
}