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:
PATH=/home/user/bin:$PATHOr you can re-add it at the end of the list with:
PATH=$PATH:/home/user/bin_______________________________
Related Articles
- Shell Functions and Path Variables by Stephen Collyer
- Distributed Compiling with distcc by Jes Hall
- Configuring Bash by David Blackman
Mitch Frazier is an Associate Editor for Linux Journal.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.
Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.
Sponsored by ActiveState
| Speed Up Your Web Site with Varnish | Jun 19, 2013 |
| Non-Linux FOSS: libnotify, OS X Style | Jun 18, 2013 |
| Containers—Not Virtual Machines—Are the Future Cloud | Jun 17, 2013 |
| Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer | Jun 12, 2013 |
| Weechat, Irssi's Little Brother | Jun 11, 2013 |
| One Tail Just Isn't Enough | Jun 07, 2013 |
- Speed Up Your Web Site with Varnish
- Containers—Not Virtual Machines—Are the Future Cloud
- Linux Systems Administrator
- Non-Linux FOSS: libnotify, OS X Style
- Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- RSS Feeds
- Reply to comment | Linux Journal
2 hours 52 min ago - Yeah, user namespaces are
4 hours 8 min ago - Cari Uang
7 hours 40 min ago - user namespaces
10 hours 33 min ago - yea
10 hours 59 min ago - One advantage with VMs
13 hours 28 min ago - about info
14 hours 1 min ago - info
14 hours 2 min ago - info
14 hours 3 min ago - info
14 hours 5 min ago
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?



Comments
Remove a path from your PATH variable (the tr/grep version)
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.
shell replacement
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
Doesn't always work
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.
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 :)
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
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".
Not extreme at all
Actually, that's not all that extreme of an example. Yours is better.
Mitch Frazier is an Associate Editor for Linux Journal.