I searched and didn't find, and had to figure stuff out... I hope to save you the trouble.

Thursday, April 23, 2009

Modifying bash $PATH

Here's some functions to edit the $PATH without leaving crud:
function pathremove()
{
local P=:$1:; shift
while [ "$1" ]; do P=${P/:$1:/:}; shift; done
P=${P#:}; P=${P%:}
echo ${P}
}

function pathprefix()
{
local P=$(pathremove $@); shift
local N=""
for n in $@; do N=${N}:$n; done
echo "${N#:}:${P}"
}

function pathsuffix()
{
local P=$(pathremove $@); shift
local N=""
for n in $@; do N=${N}:$n; done
echo "${P}${N}"
}

Example usage:
export PATH=$(pathsuffix $PATH $HOME/bin /opt/bin)
export PATH=$(pathremove $PATH /sbin)
export LD_LIBRARY_PATH=$(pathprefix $LD_LIBRARY_PATH /opt/lib)

No comments:

Post a Comment

Followers