remove a path from PATH environment variable

Hi
I need a script which will remove a path from PATH environment variable. For example

$echo PATH
/usr/local/bin:/usr/bin:test/rmve:/usr/games

$echo rmv
test/rmve

Here I need a shell script which will remove rmv path (test/rmve) from PATH (/usr/local/bin:/usr/bin:test/rmve:/usr/games) and update the PATH as /usr/local/bin:/usr/bin:/usr/games

I have used the following line for this

PATH=$(echo $PATH | sed -e 's;:\?'$rmv';;' -e 's;'$rmv':\?;;')
export $PATH

This is working fine only in Linux but not in AIX or Sun or any other OS

Can someone please help me in this?

Thanks,
Madhu.

EDIT: Use

```text
 and 
```

tags when posting code, data or logs, ty.

PATH = echo $(echo $PATH | sed -e 's;:\?'$APPLYPTF_DIR';;' -e 's;'$APPLYPTF_DIR':\?;;')

When defining a variable there may be no spaces left and right of the equal sign. This will not even work on Linux usually. Also that 1st echo outside $() is useless.
I did not check the rest but try it with this and add an export to make it work:

PATH=$(echo $PATH | sed -e 's;:\?'$APPLYPTF_DIR';;' -e 's;'$APPLYPTF_DIR':\?;;')
export PATH

Sorry, there is a typo in the thread description.. I updated it now

I used

PATH=$(echo $PATH | sed -e 's;:\?'$rmv';;' -e 's;'$rmv':\?;;')
export $PATH

Still having the same problem

Edit your last post and add code tags as already asked for.

Please post the error you get; maybe add a set -x in the head of your script for more verbose output.

Here is the out put

$ echo $PATH
/usr/local/bin:/usr/bin:test/rmve:/usr/games
$ echo $rmv
test/rmve
$ PATH=$(echo $PATH | sed -e 's;:\?'$rmv';;' -e 's;'$rmv':\?;;')
$export PATH
$ echo $PATH
/usr/local/bin:/usr/bin:test/rmve:/usr/games

There is no error reported

if I put the line in a script and execute as you suggested then the output is

$ . ./test.sh
+ + sed -e s;:\?test/rmve;; -e s;test/rmve:\?;;
+ echo /usr/local/bin:/usr/bin:test/rmve:/usr/games
PATH=/usr/local/bin:/usr/bin:test/rmve:/usr/games

the test.sh has

set -x
PATH=$(echo $PATH | sed -e 's;:\?'$rmv';;' -e 's;'$rmv':\?;;')
export PATH

You can try this:

rmv="test/rmve"
PATH=$(echo $PATH| sed -e 's!'$rmv'!!' -e 's/::/:/')
echo "PATH:" $PATH
export PATH

New code didn't work on SUN INTEL machine

I have created a shell script file test.sh with the below code

set -x
rmv="test/rmve"
PATH=$(echo $PATH| sed -e 's!'$rmv'!!' -e 's/::/:/')
echo "PATH:" $PATH
export PATH

When I run the script, it reported the below error

# . ./test.sh
syntax error: `PATH=$' unexpected
echo $PATH |awk -F":" -v v="$rmv" '{for(i=1;i<=NF;i++)if($i==v){$i=""}}1' OFS=":"

on Solaris use nawk

Is there any script which will work on all the machines?

On Solaris INTEL machine, the previous script behaves in below manner

$echo $PATH
/usr/local/bin:/usr/bin:test/rmve:/usr/games
$PATH=$(echo $PATH| sed -e 's!'$rmv'!!' -e 's/::/:/')
syntax error: `testvar=$' unexpected
$echo $PATH| sed -e 's!'$rmv'!!' -e 's/::/:/'
/usr/local/bin:/usr/bin:/usr/games

The script works just fine, but it fails to reassign PATH

rmpath() #@ remove directory or directories from $PATH
{        #@ USAGE: rmpath dir ...
    for p in "$@"
    do
      p=${p%"${p##*[!/]}"} ## remove trailing slashes
      case $PATH in
          "$p":*) PATH=${PATH#$p:} ;; ## at beginning of PATH
          *:"$p") PATH=${PATH%:$p} ;; ## at end of PATH
          *:"$p":*) PATH=${PATH%":$p"*}${PATH##*"$p"} ;;
      esac
    done
    export PATH
}
1 Like