0

I have a complicated PATH that I would like to revise, and I don't understand how to do this. I cannot even figure out how a certain directory got there. Additionally, it is there twice. Is it really this hard to edit the order of a path? Is there a way to simply edit every file that determines the path?

  • Review [How to keep from duplicating path variable in csh](http://stackoverflow.com/a/137981/15168) where my answer (linked) contains a Bourne/Korn/Bash shell script that removes duplicates names from a PATH and can be used to remove chosen entries (and you can add entries at either end of the PATH easily enough; insert entries in the middle is not directly supported). There's also [How do I manipulate path elements in shell scripts](http://stackoverflow.com/questions/273909/how-do-i-manipulate-path-elements-in-shell-scripts/274448#274448). – Jonathan Leffler Feb 09 '15 at 03:06

1 Answers1

0

not hard at all. The path is a colon-separated string stored in the process environment PATH variable. If you change the value of the string, your command search path will change. That's all there is to it.

PATH=/bin:/usr/bin:/usr/local/bin:$HOME/bin:.

The default path is created when you log in from a system default bashrc file in /etc and your locally configured .rc and .bashrc files. You can amend or edit PATH from inside your $HOME/.bashrc

Edit: in general, because it is so easy to change, every script included by the bashrc file, either directly or via another script, can modify the path. To know which one is making a particular change you have to track down the script.

Andras
  • 2,995
  • 11
  • 17
  • I looked in /etc/bashrc, ~/.bashrc, and ~/.profile, and did not find the directory that I wanted to delete. This is why I am struggling with it. Where else should I be looking? I know that /etc/paths.d also contributes to the PATH variable, but it's not there, either. – Chris Minich Feb 09 '15 at 02:18
  • try a bisection search -- print out $PATH at the top of your ~/.bashrc, to see if it was added by the system or your local rc file. Log in again (eg `ssh localhost`) and see what it says. You can then move up or down from there. – Andras Feb 09 '15 at 02:21
  • might be easier to just post-process PATH to delete the directory you don't want -- `PATH=$(echo -n $PATH | sed -e 's|:/unwelcome/path:|:|g')` – Andras Feb 09 '15 at 02:29
  • I thought I could find the directory string by using grep, but that did not work. I did use your post-process idea, just now, but it needs tweaking because its deleting BOTH instances (I just want to delete the duplicate). – Chris Minich Feb 09 '15 at 03:32
  • oh, I thought you wanted to delete both. if you leave off the `|g'` from the end of the sed -e substiution, it will only delete the first instance ('g' is the "do globally on the line" flag to the s/// substitute command of sed) – Andras Feb 09 '15 at 04:23