I added a directory path to system path($PATH
) by running,
export PATH=$PATH:/home/avinash/Desktop/raj
Now my path look like this,
$ echo $PATH /usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/avinash/Desktop/raj
I want to remove /home/avinash/Desktop/raj
from system path variable by using command(like export command does on adding path).
In your current shell (your current session of gnome-terminal) you can do this using:
In general:
deletes shortest match of
$substring
from back of$string
.Check out String manipulation for more info.
Running
export PATH=$PATH:/...
doesn't set yourPATH
system-wide. It's just a shell variable. Start a new shell and BOOM, it's gone. Obviously if you've added that to~/.bashrc
(or another environment bootstrap file) you'll have to revert that change but it doesn't sound like your problem here.If you're desperate not to start a new shell, you could set it by removing it manually, with:
If you put the
export
statement in any shell initiation file like~/.bashrc
, you can use the following commands in terminal,It will remove the folder from path.
If you have exported the path from a terminal
The folder will be in path as long as you are in that shell. To overwrite the path you have to assign new path. As oli already mentioned in the other answer.
You can use the following command to set old path
Or, simply
This is Substring Replacement,
If you want use it as a command, here is a little script:
Name it
unexport
, and add it to your PATH.Usage:
unexport /usr/local/bin /bin /sbin
This script does not change your actually
PATH
. If you want script to do it, you should change last line. Substituteecho "export PATH=$PATH"
toexport PATH=$PATH
One dirty hack is
tr
grep -v
, andpaste
.this probably wont work well if any dir in PATH has
:
or a new lineif you find yourself doing this a lot, consider making it a function and saving in your shell profile (e.g.
.bashrc
,.zshrc
)export PATH=$PATH:/home/avinash/Desktop/raj
(Here you have added the file to the path variable.)echo $PATH
If you execute this command, This is the output:
How do you remove it from the path? just execute
It is a concept of string replacement.
The shell builtin command
export
Doesn't determine whether or not a variable is set, it determines if the variable and its value are also set in sub-processes.Changing a variable, exported or not, does not affect the parent process or above, so changing it in a script has no effect unless that script is run with
source
.A shell function:
will trim off the last component of the PATH variable. Its
export
status is unaffected.declare -x PATH="/desired/path/you_want" if I'm not mistaken that itself will update what you want and it's completely at your behest what it will be. [Make sure you run as root AND as the desired user for which you want this change to affect] To be double sure, run ENV afterwards to make sure that the $PATH was effectively altered as you want. (This is usable for altering any of the output you will get when you run '/usr/bin/env'. Although I dont know whether or not it is permanent. In that respect do as stated above and alter ~/.bashrc for it to stick)
I simply use sed, like this
this should do the trick.
A little explain how this will work: First we define the variable value setting PATH=, then we must say to use the result value of next command as the value of this variable just using $(command), after came the SED command sed "s///", the / will be the separators for this command, by default we use / but you can set anything like - or _. The first s is for replace, then came the first separator, after came the expression you look for, then another separator, then the expression you want to replace the first one (that you look for), then another separator (the last one) and you can use some argument like g, for a global replace. The \ is a scape character that used for make sed understand that next / is a literal and not the separator. The <<< is for drop the variable at the command sed.