|
|
|
|
|
by nerdponx
1630 days ago
|
|
Most notably you will likely end up with duplicated elements in PATH unless you take specific steps to prevent double-adding. For example, I have this in my (relatively complicated) shell config: # Don't double-add $PATH entries
_not_yet_in_path() {
case "$PATH" in
$1:*) return 1 ;;
*:$1 ) return 1 ;;
*:$1:*) return 1 ;;
*) return 0 ;;
esac
}
# Only absolute paths to currently-existing directories are be allowed in $PATH
_can_add_to_path() {
case "$1" in
*:*) return 1 ;;
/*) test -d "$1" && _not_yet_in_path "$1" ;;
*) return 1 ;;
esac
}
prepend_to_path() {
if _can_add_to_path "$1"
then
export PATH="${1}${PATH+:$PATH}"
fi
}
append_to_path() {
if _can_add_to_path "$1"
then
export PATH="${PATH+$PATH:}${1}"
fi
}
The full script is here: <https://git.sr.ht/~wintershadows/dotfiles/tree/master/item/....>. Feedback is always welcome on how I can make this better! The Zsh version of this is a lot nicer. |
|