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.
This commit is contained in:
David Alexander 2015-10-26 15:14:25 -04:00 committed by Geoff Harcourt
parent d98d06a905
commit f7c73f7c1b
2 changed files with 16 additions and 2 deletions

View file

@ -1,5 +1,5 @@
# ensure dotfiles bin directory is loaded first
export PATH="$HOME/.bin:/usr/local/sbin:$PATH"
PATH="$HOME/.bin:/usr/local/sbin:$PATH"
# load rbenv if available
if command -v rbenv >/dev/null; then
@ -7,4 +7,8 @@ if command -v rbenv >/dev/null; then
fi
# mkdir .git/safe in the root of repositories you trust
export PATH=".git/safe/../../bin:$PATH"
PATH=".git/safe/../../bin:$PATH"
typeset -U PATH
export PATH

View file

@ -0,0 +1,10 @@
# 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
}