63 lines
1.5 KiB
Bash
63 lines
1.5 KiB
Bash
# ~/.config/posix-common/env_functions.sh
|
|
LOCATION="posix-common/env_functions.sh"
|
|
|
|
# {{{ === Script Debug Settings ===
|
|
#DEBUG=y # Comment this out to disable DEBUGing.
|
|
_mordu() {
|
|
[ "$DEBUG" ] && echo ">>> Mordu@$LOCATION: $*"
|
|
}
|
|
_mordu "Beginning processing $LOCATION."
|
|
# }}} === Script Debug Settings ===
|
|
|
|
# {{{ === PATH Management Functions ===
|
|
_mordu "Creating PATH management functions."
|
|
# Usage: indirect_expand PATH -> $PATH
|
|
indirect_expand () {
|
|
env |sed -n "s/^$1=//p"
|
|
}
|
|
|
|
# Usage: pathremove /path/to/bin [PATH]
|
|
# Eg, to remove ~/bin from $PATH
|
|
# pathremove ~/bin PATH
|
|
pathremove () {
|
|
local IFS=':'
|
|
local newpath
|
|
local dir
|
|
local var=${2:-PATH}
|
|
# Bash has ${!var}, but this is not portable.
|
|
for dir in $(indirect_expand "$var"); do
|
|
IFS=''
|
|
if [ "$dir" != "$1" ]; then
|
|
newpath="$newpath:$dir"
|
|
fi
|
|
done
|
|
export "$var"="${newpath#:}"
|
|
}
|
|
|
|
# Usage: pathprepend /path/to/bin [PATH]
|
|
# Eg, to prepend ~/bin to $PATH
|
|
# pathprepend ~/bin PATH
|
|
pathprepend () {
|
|
# if the path is already in the variable,
|
|
# remove it so we can move it to the front
|
|
pathremove "$1" "$2"
|
|
#[ -d "${1}" ] || return
|
|
local var="${2:-PATH}"
|
|
local value=$(indirect_expand "$var")
|
|
export "${var}=${1}${value:+:${value}}"
|
|
}
|
|
|
|
# Usage: pathappend /path/to/bin [PATH]
|
|
# Eg, to append ~/bin to $PATH
|
|
# pathappend ~/bin PATH
|
|
pathappend () {
|
|
pathremove "${1}" "${2}"
|
|
#[ -d "${1}" ] || return
|
|
var=${2:-PATH}
|
|
value=$(indirect_expand "$var")
|
|
export "$var=${value:+${value}:}${1}"
|
|
}
|
|
# }}} === PATH Management Functions ===
|
|
|
|
_mordu "Finished processing $LOCATION."
|