remove a path from PATH environment variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting remove a path from PATH environment variable
# 1  
Old 10-14-2009
remove a path from PATH environment variable

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

Code:
$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
Code:
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 [code] and [/code] tags when posting code, data or logs, ty.

Last edited by madhu84; 10-14-2009 at 07:40 AM.. Reason: code tags
# 2  
Old 10-14-2009
Code:
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:
Code:
PATH=$(echo $PATH | sed -e 's;:\?'$APPLYPTF_DIR';;' -e 's;'$APPLYPTF_DIR':\?;;')
export PATH

# 3  
Old 10-14-2009
Sorry, there is a typo in the thread description.. I updated it now


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

Still having the same problem

Last edited by madhu84; 10-14-2009 at 07:47 AM..
# 4  
Old 10-14-2009
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.
# 5  
Old 10-14-2009
Here is the out put

Code:
$ 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

Code:
$ . ./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

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


Last edited by madhu84; 10-14-2009 at 08:00 AM..
# 6  
Old 10-14-2009
You can try this:
Code:
rmv="test/rmve"
PATH=$(echo $PATH| sed -e 's!'$rmv'!!' -e 's/::/:/')
echo "PATH:" $PATH
export PATH

# 7  
Old 10-23-2009
New code didn't work on SUN INTEL machine

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

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

Code:
# . ./test.sh
syntax error: `PATH=$' unexpected

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function to remove the directories from PATH variable

Hello, From the URL https://www.unix.com/shell-programming-scripting/121303-remove-path-path-environment-variable-2.html I got a function to remove the directories from a path. but looks like this isnt quite working.. i am also not able to post the comments in the thread as it is closed. ... (6 Replies)
Discussion started by: satishkumar432
6 Replies

2. Ubuntu

PATH environment variable

PATH is an environment variable. When I open a terminal say terminal 1 and set some path in PATH variable it gets set which I can see using ech $PATH. But when I open a new terminal say terminal 2 and fire echo $PATH why cannot I see the same output as seen in terminal terminal 1? Why the path... (4 Replies)
Discussion started by: rupeshkp728
4 Replies

3. Shell Programming and Scripting

Path a variable to sed that includes a path

Hi I'm trying to select text between two lines, I'm using sed to to this, but I need to pass variables to it. For example start="BEGIN /home/mavkoup/data" end="END" sed -n -e '/${start}/,/${end}/g' doesn't work. I've tried double quotes as well. I think there's a problem with the / in the... (4 Replies)
Discussion started by: mavkoup
4 Replies

4. Shell Programming and Scripting

Appending a path in user's PATH variable

Hello Folks, I want to append a path in user's PATH variable which should be available in current session. Background Numerous persons will run a utility. Aim is to add the absolute path of the utility the first time it runs so that next runs have the PATH in env & users can directly run... (6 Replies)
Discussion started by: vibhor_agarwali
6 Replies

5. Shell Programming and Scripting

one liner to extract path from PATH variable

Hi, Could anyone help me in writing a single line code by either using (sed, awk, perl or whatever) to extract a specific path from the PATH environment variable? for eg: suppose the PATH is being set as follows PATH=/usr/bin/:/usr/local/bin:/bin:/usr/sbin:/usr/bin/java:/usr/bin/perl3.4 ... (2 Replies)
Discussion started by: royalibrahim
2 Replies

6. UNIX for Dummies Questions & Answers

Path Environment

How do we change path environment? (2 Replies)
Discussion started by: mehmetned
2 Replies

7. Shell Programming and Scripting

problem in getting the path of environment variable set in bashrc in my shell script

hi all i have joined new to the group. i have set an variable in my bashrc file. .bashrc PROGHOME=/home/braf/braf/prog export PROGHOME but while using it in my shell script its path is not taken and i had to explicitly give the export command to set the path. in my script... (8 Replies)
Discussion started by: krithika
8 Replies

8. Shell Programming and Scripting

:: in PATH environment variable

whats the meaning of :: colon in PATH environment? /usr/local/bin:/usr/bin:/usr/local/gnu/bin::.:/usr/local/bin:/usr/bin:/usr/local/gnu/bin:/usr/local/bin and whats the meaning of // in PATH ? (1 Reply)
Discussion started by: gfhgfnhhn
1 Replies

9. Shell Programming and Scripting

Path Environment Variable

Hi..... I'm kind of new to c programming in Unix...need help here. Supposed to write a source code to support Path environment variable for my programming assignment for Spring semester. but i'm kind of stuck. Could anyone out there assist me? prompt> /bin/ls My program could output... (3 Replies)
Discussion started by: tancy
3 Replies

10. UNIX for Advanced & Expert Users

How does the PATH and MANPATH environment variable get set?

Hi, How does the PATH and MANPATH environment variable get set? I want to add "/opt/SUNWspro/bin" to the search path for all the users. Where can I access this variable. I know in my home directory, depend on which shell I use, there are files such as .profile and .cshrc which I can edit to... (3 Replies)
Discussion started by: vtran4270
3 Replies
Login or Register to Ask a Question