Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 12-14-2008, 12:12 AM   #1
Registered: Sep 2005
Location: New Jersey, USA
Distribution: Centos 7 , Linux Mint 18.1 Under VMware in Win7
Posts: 537

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?


Last edited by swamprat; 12-14-2008 at 12:13 AM.
Old 12-14-2008, 12:35 AM   #2
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.
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:


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

I didn't test this, so be warned.
Old 12-14-2008, 02:10 AM   #3
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:

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.


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:

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

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

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 04:11 PM
Image Path reference in Linux (Absolute path) javabuddy Linux - General 7 06-05-2006 08:45 AM
script to change unix path to windows path in all files csross Programming 8 04-29-2006 02:05 PM
Why is the value of $PATH in console mode different from the $PATH in xterm emulator? Akhran Debian 9 03-09-2006 07:10 PM
How to Chnage Python's module search path (sys.path)? lramos85 Linux - Software 1 05-02-2004 07:10 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:31 AM.

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