Как удалить path linux

Remove redundant paths from $PATH variable

I have defined the same path in the $PATH variable 6 times. I wasn’t logging out to check whether it worked. How can I remove the duplicates? The $PATH variable looks like this:

echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/flacs/Programmes/USFOS/bin:/home/flacs/Programmes/USFOS/bin:/home/flacs/Programmes/USFOS/bin:/home/flacs/Programmes/USFOS/bin:/home/flacs/Programmes/USFOS/bin:/home/flacs/Programmes/USFOS/bin 
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games 

17 Answers 17

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games 

that would be for the current session, if you want to change permanently add it to any .bashrc, bash.bashrc, /etc/profile — whatever fits your system and user needs.

Note: This is for Linux. We’ll make this clear for new coders. (` , ‘) Don’t try to SET = these.

yes, I set them permanently in bash.bashrc. So should the command be something like this? echo ‘export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games’ >> ~/.bashrc

the thing is that depending on your OS a chain of configurations are executed. You need to make sure the PATH variable is not overwritten later. The easiest way to do that (for one user) is to overwrite it in the user’s personal .bashrc, which commonly is located in his home directory.

your command sets the PATH to — $PATH(the current value of PATH) + the string /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/gam‌​es. If you want to have just the String, remove $PATH + the semicolon (:) from your command. It doesn’t matter if you use echo or edit the file ~/.bashrc by hand.

the bash.bashrc file is located in the /etc folder. It does not display the $PATH variable though, so I’m not sure where to edit it

in your first comment you echo to ~/.bashrc (quoting: >> ~/.bashrc), not to /etc/bash.bashrc. If you want to change the PATH for a specific user edit it in /home//.bashrc file. the /etc/bash.bashrc applies to all users.

If you’re using Bash, you can also do the following if, let’s say, you want to remove the directory /home/wrong/dir/ from your PATH variable:

PATH=`echo $PATH | sed -e 's/:\/home\/wrong\/dir\/$//'` 

This was useful to me, as a directory was added in /etc/profile which I wished to exclude, but have no write access to /etc. Thanks 🙂

Protip: you can use different deliminator in the sed expression to avoid the \/ escaping: PATH=$(echo $PATH | sed -e ‘s|:/home/wrong/dir|$||’)

This trick helps when I want to have new PATH right away, and don’t want to log out of current terminal. However, to avoid messing up, one should experiment with the PATH generating command (i.e. echo $PATH | sed -e ‘s/:\/home\/wrong\/dir\/$//’ ) before assigning it to PATH.

This won’t work when the path to be deleted happens to be the 1st path in $PATH . Use this one: PATH=$(echo :$PATH: | sed -e ‘s,:/home/wrong/dir. g’ -e ‘s/^://’ -e ‘s/:$//’)

didn’t work $ PATH= echo $PATH | sed -e ‘s/:\/scratch\/sjn\/anaconda\/bin\/python\/$//’« for removing /scratch/sjn/anaconda/bin/python

Linux: Remove redundant paths from $PATH variable

Linux From Scratch has this function in /etc/profile

# Functions to help us manage paths. Second argument is the name of the # path variable to be modified (default: PATH) pathremove () < local IFS=':' local NEWPATH local DIR local PATHVARIABLE=$for DIR in $ ; do if [ "$DIR" != "$1" ] ; then NEWPATH=$$DIR fi done export $PATHVARIABLE="$NEWPATH" > 

This is intended to be used with these functions for adding to the path, so that you don’t do it redundantly:

pathprepend () < pathremove $1 $2 local PATHVARIABLE=$export $PATHVARIABLE="$1$>" > pathappend () < pathremove $1 $2 local PATHVARIABLE=$export $PATHVARIABLE="$:>$1" > 

Simple usage is to just give pathremove the directory path to remove — but keep in mind that it has to match exactly:

$ pathremove /home/username/anaconda3/bin 

This will remove each instance of that directory from your path.

Читайте также:  Консоль linux как вызвать

If you want the directory in your path, but without the redundancies, you could just use one of the other functions, e.g. — for your specific case:

$ pathprepend /usr/local/sbin $ pathappend /usr/local/bin $ pathappend /usr/sbin $ pathappend /usr/bin $ pathappend /sbin $ pathappend /bin $ pathappend /usr/games 

But, unless readability is the concern, at this point you’re better off just doing:

$ export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games 

Would the above work in all shells known to man?

I would presume the above to work in sh , dash , and bash at least. I would be surprised to learn it doesn’t work in csh , fish’, or ksh`. I doubt it would work in Windows command shell or Powershell.

If you have Python, the following sort of command should do what is directly asked (that is, remove redundant paths):

$ PATH=$( python -c " import os path = os.environ['PATH'].split(':') print(':'.join(sorted(set(path), key=path.index))) " ) 

A one-liner (to sidestep multiline issues):

$ PATH=$( python -c "import os; path = os.environ['PATH'].split(':'); print(':'.join(sorted(set(path), key=path.index)))" ) 

The above removes later redundant paths. To remove earlier redundant paths, use a reversed list’s index and reverse it again:

$ PATH=$( python -c " import os path = os.environ['PATH'].split(':')[::-1] print(':'.join(sorted(set(path), key=path.index, reverse=True))) " ) 

+1. does pathremove() work in all shell flavors, like, csh, ksh, bash etc? btw, my /etc/profile on RHEL doesn’t have partremove(), but only has pathmunge() .

@Tagar I make no guarantees about compatibility with all other shells. I suggest testing it out on whatever shell you’re using, and if it doesn’t work for your shell, you can use Python if you have it installed — I added some Python to the answer to describe how.

If I read «3.5.3 Shell Parameter Expansion» correctly, $ is a kind of variable indirection, but I’m not sure how it works here. Could you explain it please ?

@loxaxs That’s a local variable in those bash functions, declared with the local builtin (which can only be used inside a function.) If you have further questions, you should probably ask a new question on the site (after searching for it first to ensure you’re not creating an exact duplicate. ).

This is a really interesting answer, thanks for the writeup! However, the original question of cleaning up a messed up PATH variable seems not to be solved elegantly (althoug, I admit, then when using this functions one would not end up with a messed up PATH to begin with).

Here is a one line code that cleans up the PATH

  • It does not disturb the order of the PATH, just removes duplicates
  • Treats : and empth PATH gracefully
  • No special characters used, so does not require escape
  • Uses /bin/awk so it works even when PATH is broken
export PATH="$(echo "$PATH" |/bin/awk 'BEGIN else>')"; 

if you don’t have awk installed it cleans your path, I advise copying your path to a txt file before with echo $PATH

This one works! except the awk is at a different place. Can just call awk directly if you use this script at the end of your bash/zshrc file. export PATH=»$(echo «$PATH» | awk ‘BEGIN else>’)»;

I’ve tested the following one-liner on a Mac. If awk isn’t where it is expected, the script doesn’t run. if [[ -x /usr/bin/awk ]]; then export PATH=»$(echo «$PATH» |/usr/bin/awk ‘BEGIN else>’)»; fi

  1. Just echo $PATH
  2. copy details into a text editor
  3. remove unwanted entries
  4. PATH= # pass new list of entries

I did this and manually pressed return at every ‘:’ splitting path variables but then don’t forget to manually concatenate into one string again as newlines are picked up.

If you just want to remove any duplicate paths, I use this script I wrote a while back since I was having trouble with multiple perl5/bin paths:

#!/bin/bash # # path-cleanup # # This must be run as "source path-cleanup" or ". path-cleanup" # so the current shell gets the changes. pathlist=`echo $PATH | sed 's/:/\n/g' | uniq` # echo "Starting PATH: $PATH" # echo "pathlist: $pathlist" unset PATH # echo "After unset, PATH: $PATH" for dir in $pathlist do if test -d $dir ; then if test -z $PATH; then PATH=$dir else PATH=$PATH:$dir fi fi done export PATH # echo "After loop, PATH: $PATH" 

And I put it in my ~/.profile at the end. Since I use BASH almost exclusively, I haven’t tried it in other shells.

+1 for the solution. Btw, it will only remove duplication paths if they’re going in list one after another. You could change |uniq to |sort|uniq to fix this, but this will change order of all directories in the path which I don’t think is a desirable side-effect.

Simple and Fool-Proof

Below is a version of this popular answer with additional explanations. It requires you to remove any duplicate or unwanted PATH components by hand. While this isn’t very elegant it may often be far simpler and fool-proof.

  1. echo $PATH > path.txt writes current path to text file
  2. Edit path.txt (e.g. using nano path.txt )
  3. export PATH=$(cat path.txt) sets path to be content of the file
  4. rm path.txt removes the file

Note: This is all for your current session, in case you wanted to change PATH permanently, you would have to adapt your bash set-up files (e.g. ~/.bashrc).

Or in this case (as the replace bit is empty) just:

I came here first but went else ware as I thought there would be a parameter expansion to do this. Easier than sed!.

How did you add these duplicate paths to your PATH variable? You must have edited one of your . files. ( .tcshrc , or .bashrc , etc depending on your particular system/shell). The way to fix it is to edit the file again and remove the duplicate paths.

If you didn’t edit any files, and you you must have modified the PATH interactively. In that case the changes won’t «stick», ie if you open another shell, or log out and log back in, the changes will be gone automatically.

Note that there are some system wide config files too, but it’s unlikely you modified those, so most likely you’ll be changing files in your personal home directory (if you want to make those changes permanent once you settle on a set of paths)

Источник

How to remove PATH variable on Ubuntu 14.04?

enter image description here

I’ve export PATH=»~/.composer/vendor/bin/lumen» in .bashrc and .bash_profile after export then lumen command not working, Rasel Khan:~$ lumen lumen: command not found But if command type ~/.composer/vendor/bin/lumen in terminal then working, i want only when lumen command type in terminal then exactly same as screenshot. how can i fix this ? see screenshot

4 Answers 4

Your problem will not be solved simply by unsetting PATH, as you’ll still be left without a PATH that includes the necessary system directories. When you set your own PATH, in most cases you will want to append your new entry to the old PATH variable, not replace it entirely, as you have done.

Set your PATH variable back to the system default by typing

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 

and then go edit your .bashrc and .bash_profile to have the correct entry, which will be something like

export PATH="$PATH:~/.composer/vendor/bin" 

Notice the variable is set to begin with the existing $PATH. This way, you’ll still have all the original system directories in your PATH, and your addition will be on the end. Also note that I removed lumen from the end of your example, because lumen is apparently the name of the binary you are trying to execute, and your PATH should include only directories containing binaries, not the binaries themselves.

thanks, i’ve now default system, but i want to working this comman d export PATH=»$PATH:~/.composer/vendor/bin/lumen , how can i fix this so when command type lumen in terminal and with working

any command not working like ls . etc, i want to remove only this path export PATH=»~/.composer/vendor/bin/lumen» in ` .bashrc` and .bash_profile

See my edited remarks above. I hadn’t noticed earlier that lumen was the name of your binary, not the name of the directory containing your binaries.

Exec this to get your system default:

the PATH stores all the places where the terminal looks for your applications/scripts etc. If you set the PATH to some «unfriendly» directory, the system will not be able to find /bin/sudo and others. You will have to type the whole path like /bin/sudo. The best way to fix this is to open your bash_profile and insert this line: export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games I think changing the profile will work just after the next reboot, so do this and after that use the command export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games to fix it for the current session.

«Protip 😀 :D» if you want to check for your own programs in a directory «/home/username/foo/bin», just add this way to your PATH and you will be able to call the programs stored there just by their names

Источник

Оцените статью
Adblock
detector