I’m a little OCD about my $PATH
…
cleanpath () {
local path p result
local -A seen
PATH=${PATH//::/:}
IFS=":" read -r -a path <<< "$PATH"
for p in "${path[@]}"; do
if [ -z "${seen[$p]}" ]; then
result="$result:$p"
seen["$p"]=1
fi
done
PATH=${result:1}
}
No, really. I’ve been using these little functions to set up my
environment for so long that they used to have sed
and perl
calls
in them, because you couldn’t do all the string-hacking internally in
ancient versions of Bash/SH:
# addpath /x/y/bin first|last
# addpath /x/y/bin before|after /z/bin
#
addpath () {
local pTMP=":$PATH:"
case "$2" in
after)
pTMP=${pTMP/:$3:/:$3:$1:}
;;
before)
pTMP=${pTMP/:$3:/:$1:$3:}
;;
first)
pTMP=:$1$pTMP
;;
last)
pTMP=$pTMP$1:
;;
*)
echo "usage: addpath /foo/bin first|last|before|after /bar/bin"
;;
esac
pTMP=${pTMP#:}
pTMP=${pTMP%:}
if [ ! -z "$pTMP" ]; then
PATH=$pTMP
fi
}
# delpath /x/y/bin
#
delpath () {
local pTMP=":$PATH:"
pTMP=${pTMP/:$1:/:}
pTMP=${pTMP#:}
pTMP=${pTMP%:}
if [ ! -z "$pTMP" ]; then
PATH=$pTMP
fi
}
(and by “used to” I mean “until yesterday”, because I just rewrote them)
Markdown formatting and simple HTML accepted.
Sometimes you have to double-click to enter text in the form (interaction between Isso and Bootstrap?). Tab is more reliable.