bash: shorten PATH manipulation

This commit is contained in:
Fernando Schauenburg 2021-02-25 19:39:59 +01:00
parent 768e53ec2e
commit e5f4c25593

View file

@ -34,7 +34,7 @@ export PYTHONSTARTUP="$XDG_CONFIG_HOME/python/startup.py"
# shellcheck disable=SC1091 # /etc/profile is provided by macOS. # shellcheck disable=SC1091 # /etc/profile is provided by macOS.
[ "$(uname)" == "Darwin" ] && { PATH=""; source /etc/profile; } [ "$(uname)" == "Darwin" ] && { PATH=""; source /etc/profile; }
_prepend_path() { # prepend $1 to variable $2 using : as separator _prepend_path() { # prepend $1 to var $2 avoiding duplicates using : as separator
if [ -d "$1" ] && [ -n "$2" ]; then if [ -d "$1" ] && [ -n "$2" ]; then
local _path="${!2}" # get path variable value local _path="${!2}" # get path variable value
case ":$_path:" in case ":$_path:" in
@ -46,26 +46,16 @@ _prepend_path() { # prepend $1 to variable $2 using : as separator
} }
# Add custom bin dirs to PATH if they exist and are not already in PATH. # Add custom bin dirs to PATH if they exist and are not already in PATH.
while read -r dir; do _prepend_path "$dir" PATH; done <<EOL
$LOCAL_PREFIX/bin
$LOCAL_PREFIX/opt/man-db/libexec/bin
$LOCAL_PREFIX/opt/coreutils/libexec/gnubin
$LOCAL_PREFIX/opt/gnu-sed/libexec/gnubin
$HOME/.local/bin
EOL
# Prepend custom man directories to MANPATH if they exist, so that we get # Prepend custom man directories to MANPATH if they exist, so that we get
# correct man page entries when multiple versions of a command are # correct man page entries when multiple versions of a command are
# available. # available.
while read -r dir; do _prepend_path "$dir" MANPATH; done <<EOL while read -r var dir; do _prepend_path "$dir" "$var"; done <<EOL
$LOCAL_PREFIX/share/man PATH $LOCAL_PREFIX/bin
$LOCAL_PREFIX/opt/man-db/libexec/man MANPATH $LOCAL_PREFIX/share/man
$LOCAL_PREFIX/opt/coreutils/libexec/gnuman PATH $HOME/.local/bin
$LOCAL_PREFIX/opt/gnu-sed/libexec/gnuman MANPATH $HOME/.local/share/man
$HOME/.local/share/man
EOL EOL
unset var dir _prepend_path
unset dir _prepend_path
# This check has to be done after PATH manipulation above so we can find brew. # This check has to be done after PATH manipulation above so we can find brew.
if command -v brew >/dev/null 2>&1; then if command -v brew >/dev/null 2>&1; then