LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-13-2008, 11:12 PM   #1
swamprat
Member
 
Registered: Sep 2005
Location: New Jersey, USA
Distribution: VMware V12 and V15 in Windows 10, MX Linux 23.1, Kubuntu 23.10, IBM z/VM 5.4
Posts: 558

Rep: Reputation: 34
Deleting a Path from $PATH


Some one was good enough to help me out with setting a new path in the $PATH variable:

"export PATH=/some/new/path/bin:$PATH"

Now I would like to delete a path from this variable ... how would this be done?

thanks

Last edited by swamprat; 12-13-2008 at 11:13 PM.
 
Old 12-13-2008, 11:35 PM   #2
penguiniator
Member
 
Registered: Feb 2004
Location: Olympia, WA
Distribution: SolydK
Posts: 442
Blog Entries: 3

Rep: Reputation: 60
This is a stickier problem than I thought it would be. I found a snippet on LJ that seems to answer your question.

http://www.linuxjournal.com/content/...th-variable-0:
Quote:
If you need to remove a path from the PATH variable before your script runs add this to the beginning of the script:

PATH=$(echo $PATH | sed -e 's;:\?/home/user/bin;;' -e 's;/home/user/bin:\?;;')

If you need, you can re-add it at the front of the list with:

PATH=/home/user/bin:$PATH

Or you can re-add it at the end of the list with:

PATH=$PATH:/home/user/bin
I didn't test this, so be warned.
 
Old 12-14-2008, 01:10 AM   #3
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
I wasn't aware that sed could use a question mark to mean "zero or one instances of preceding character" (appears to be an error in my copy of Linux in a Nutshell), so I was hoping the Linux Journal reference would explain the logic. It didn't. (BTW, here is the link w/o the colon tagged on the end. ) But when I tried the command out it didn't seem to work. And playing around with removing the backslashes (which I thought were wrong) only got me completely confused. So if somebody can give me a coherent explanation of the use of those questions marks and (possibly) backslashes, I would appreciate it. In the meantime, here is a version I understand and I am pretty sure it works:

Code:
PATH=$(echo :$PATH | sed "s@:/path/to/remove@@; s@^:@@")
Explanation: I add a colon before $PATH so that first path element is not special. The sed command deletes the undesired path along with its preceding colon and then deletes the (remaining) initial colon for the string. The same idea could be applied with a colon added to the end instead, with appropriate changes to the sed command.

EDIT:

Upon further reflection I realized that if the path element to delete matches part of another path element, the above statement could result in doing something other than what was intended. (I believe the statement from the LJ article also suffers from this.) This problem can be avoided by bracketing the element to delete with colons on both sides:

Code:
PATH=$(echo :$PATH: | sed "s@:/path/to/remove:@:@; s@^:@@; s@:$@@")

Last edited by blackhole54; 12-15-2008 at 10:38 PM.
 
Old 12-14-2008, 01:49 AM   #4
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,623

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
the easiest way might be to remove the path from ~/.bash_profile if it is in there
or from/etc/bash_profile
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
export Path=/usr/java:$Path does not seem to work lumix Linux - Newbie 1 03-19-2007 03:11 PM
Image Path reference in Linux (Absolute path) javabuddy Linux - General 7 06-05-2006 07:45 AM
script to change unix path to windows path in all files csross Programming 8 04-29-2006 01:05 PM
Why is the value of $PATH in console mode different from the $PATH in xterm emulator? Akhran Debian 9 03-09-2006 06:10 PM
How to Chnage Python's module search path (sys.path)? lramos85 Linux - Software 1 05-02-2004 06:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration