This repository has been archived on 2022-02-20. You can view files and clone it, but cannot push or open issues/pull-requests.
bash-shell-base/function/02-path

50 lines
1.1 KiB
Bash

#!/bin/bash
path_remove () {
local IFS=':'
local NEWPATH
local DIR
local PATHVARIABLE=${2:-PATH}
for DIR in ${!PATHVARIABLE} ; do
if [ "$DIR" != "$1" ] ; then
NEWPATH=${NEWPATH:+$NEWPATH:}$DIR
fi
done
export $PATHVARIABLE="$NEWPATH"
}
path_prepend () {
path_remove $1 $2
if [ -d "$1" ]; then
local PATHVARIABLE=${2:-PATH}
export $PATHVARIABLE="$1${!PATHVARIABLE:+:${!PATHVARIABLE}}"
fi
}
path_append () {
path_remove $1 $2
if [ -d "$1" ]; then
local PATHVARIABLE=${2:-PATH}
export $PATHVARIABLE="${!PATHVARIABLE:+${!PATHVARIABLE}:}$1"
fi
}
path_insert () {
if [ -d "$1" ]; then
local PATHVAR=${4:-PATH}
local temp
echo inserting $1 into ${!PATHVAR} $2 $3
boa=$([[ $2 == b* ]] && echo "$1:&" || echo "&:$1:")
temp="$(echo ${!PATHVAR} | sed -r "s#$3(:)?#$boa#" | sed 's#^:##' | sed 's#\:$##' | sed 's#::#:#g' )"
echo result: $temp
export PATHVAR=$temp
fi
}
# path_insert_before () {
# _path_insert b "$@"
# }
#
# path_insert_after () {
# _path_insert a "$@"
# }