Remove a path from your PATH variable

April 15th, 2008 by Mitch Frazier in

Your rating: None Average: 5 (1 vote)

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

_______________________________
Related Articles __________________________

Mitch Frazier is an Associate Editor for Linux Journal and the Web Editor for linuxjournal.com.


Special Magazine Offer -- Free Gift with Subscription
Receive a free digital copy of Linux Journal's System Administration Special Edition as well as instant online access to current and past issues. CLICK HERE for offer

Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I've always been fond of the following structure:

RPATH="/home/user/bin"
PATH=$( echo ${PATH} | tr -s ":" "\n" | grep -vwE "(${RPATH})" | tr -s "\n" ":" | sed "s/:$//" )

Split path on ":", one per line. Squeeze double occurences of ":".
Remove line(s) that exactly matches path(s) to remove
Join list of paths to a new PATH. Squeeze double occurences of "\n".
Remove ":" at end of line.

If you need to remove more than one path, You just add to the RPATH variable

RPATH="/home/user/bin|/usr/games"

Why squeeze? If You have an empty path element, then current directory seems to be included. This can lead to call of unexpected programs.

higuita's picture

shell replacement

On April 21st, 2008 higuita (not verified) says:

you can also try the variable replacement that bash have:

echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin

echo ${PATH/\/usr\/sbin:}
/sbin:/bin:/usr/bin

that is this works like ${variable/text-to-remove-in-this-variable}

so $PATH=${PATH/\/usr\/sbin:}

you can also do a find and replace:

echo ${PATH/\/usr\/sbin:/\/usr\/local\/sbin:}
/sbin:/bin:/usr/local/sbin:/usr/bin

the nasty part here is the need to escape the / character, but works well :)

higuita

Mitch Frazier's picture

Doesn't always work

On April 22nd, 2008 Mitch Frazier says:

What about the case where the path you want to remove is at the end? Including the colon in the pattern misses that case.

__________________________

Mitch Frazier is an Associate Editor for Linux Journal and the Web Editor for linuxjournal.com.

stevenworr's picture

Editing PATH variables.

On April 16th, 2008 stevenworr says:

A much easier way to go is to let the builtin readline functions do more of the work.
Add this to your .inputrc

"\C-xp": "PATH=${PATH}\e\C-e\C-a\ef\C-f"

What it says in english is: "If I hit ^Xp, then on the commandline say PATH=$PATH. Then use escape-control-e
to cause any variables to be expanded. Then go to the beginning of the line, then go forwards by one word and then go forward by one character."

This will leave your cursor right on the first character of the value of your PATH.

After you add your sequence to the .inputrc, just say ^X^R to cause your .inputrc to be re-read.

__________________________

Steven W. Orr

Mitch Frazier's picture

If you say so :)

On April 22nd, 2008 Mitch Frazier says:

I'll take your word for that one. Although, I was thinking more in terms of having this in a script to take out paths you don't want, and not in terms of doing it interactively at the command line.

__________________________

Mitch Frazier is an Associate Editor for Linux Journal and the Web Editor for linuxjournal.com.

slu's picture

That doesn't always work

On April 15th, 2008 slu says:

Testing the above with

PATH=/home/user/bin:/one:/home/user/bin:/two:/home/user/bin

I'm getting the following result:

:/one:/two:/home/user/bin

Instead I would suggest the following

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

that gives us the following result:

/one:/two

I know my test PATH is a little extreme, as you shouldn't normally see the same path more than once in your PATH. But real life systems aren't always "normal".

Mitch Frazier's picture

Not extreme at all

On April 22nd, 2008 Mitch Frazier says:

Actually, that's not all that extreme of an example. Yours is better.

__________________________

Mitch Frazier is an Associate Editor for Linux Journal and the Web Editor for linuxjournal.com.

Post new comment

Please note that comments may not appear immediately, so there is no need to repost your comment.
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <i> <b>
  • Lines and paragraphs break automatically.

More information about formatting options

Newsletter

Each week Linux Journal editors will tell you what's hot in the world of Linux. You will receive late breaking news, technical tips and tricks, and links to in-depth stories featured on www.linuxjournal.com.
Sign up for our Email Newsletter

Tech Tip Videos

From the Magazine

July 2009, #183

News Flash: Linux Kernel 3.0 to include an on-the-go Expresso machine interface! Ok, maybe not, but Linux is definitely going mobile, from phones to e-readers. Find out more inside about Android, the Kindle 2, the Western Digital MyBook II, The Bug, and Indamixx (a portable recording studio). And if you've gone mobile and you been wanting more Emacs in your life then check out Conkeror.


To compliment the mobile we've got the stationary: parsing command line options with getopt, checking your Ruby code with metric_fu, and building a secure Squid proxy. How is this stationary you ask? What can we say? It's not. We just wanted to see if anybody actually read this part of the page :) .


All this and more, and all you have to do is get your hot sweaty hands on the latest copy of Linux Journal.





Read this issue