Remove a path from your PATH variable


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:

Related Articles

Mitch Frazier is an Associate Editor for Linux Journal.


Comment viewing options

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

Remove a path from your PATH variable (the tr/grep version)

Bjoern's picture

I've always been fond of the following structure:

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


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

shell replacement

higuita's picture

you can also try the variable replacement that bash have:

echo $PATH

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

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:}

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


Doesn't always work

Mitch Frazier's picture

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.

Editing PATH variables.

stevenworr's picture

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

If you say so :)

Mitch Frazier's picture

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.

That doesn't always work

slu's picture

Testing the above with


I'm getting the following result:


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:


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".

Not extreme at all

Mitch Frazier's picture

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

Mitch Frazier is an Associate Editor for Linux Journal.

Geek Guide
The DevOps Toolbox

Tools and Technologies for Scale and Reliability
by Linux Journal Editor Bill Childers

Get your free copy today

Sponsored by IBM

8 Signs You're Beyond Cron

Scheduling Crontabs With an Enterprise Scheduler
On Demand
Moderated by Linux Journal Contributor Mike Diehl

Sign up and watch now

Sponsored by Skybot