From d91dca49902093b67d62179ca6ae857693815e11 Mon Sep 17 00:00:00 2001 From: Bodaniel Jeanes Date: Wed, 4 Jan 2012 21:41:41 -0600 Subject: [PATCH] Dynamically build path without duplicates and only for existing directories --- shells/bootstrap.sh | 4 +--- shells/path.sh | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 shells/path.sh diff --git a/shells/bootstrap.sh b/shells/bootstrap.sh index 78ea2b8..f880a77 100644 --- a/shells/bootstrap.sh +++ b/shells/bootstrap.sh @@ -1,8 +1,6 @@ COMMON_SHELL_FILES="$SHELL_FILES/../common" -# TODO: Remove duplicates from the PATH -PATH="/usr/local/bin:/usr/local/sbin:/usr/X11/bin:/usr/bin:/usr/sbin:/bin:/sbin:${PATH}" - +source "$SHELL_FILES/../path.sh" source "$SHELL_FILES/../tmux.sh" CDPATH=".:${HOME}" diff --git a/shells/path.sh b/shells/path.sh new file mode 100644 index 0000000..c0a125e --- /dev/null +++ b/shells/path.sh @@ -0,0 +1,20 @@ +function __append_path { + if [ -z "$(eval echo \$$1)" ]; then + eval "$1=\"$2\"" + else + eval "$1=\"\$$1:$2\"" + fi +} + +[ -d "$HOME/bin" ] && __append_path "new_path" "$HOME/bin" +[ -d "$DOT_FILES/bin" ] && __append_path "new_path" "$DOT_FILES/bin" +[ -d '/usr/local/bin' ] && __append_path "new_path" "/usr/local/bin" +[ -d '/usr/local/sbin' ] && __append_path "new_path" "/usr/local/sbin" +[ -d '/opt/local/bin' ] && __append_path "new_path" "/opt/local/bin" +[ -d '/opt/local/sbin' ] && __append_path "new_path" "/opt/local/sbin" +[ -d '/usr/X11/bin' ] && __append_path "new_path" "/usr/X11/bin" +[ -d '/usr/bin' ] && __append_path "new_path" "/usr/bin" +[ -d '/usr/sbin' ] && __append_path "new_path" "/usr/sbin" +[ -d '/bin' ] && __append_path "new_path" "/bin" +[ -d '/sbin' ] && __append_path "new_path" "/sbin" +PATH="$new_path"