Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I debug $PATH?

Tags:

bash

macos

There is a directory in my $PATH that I want removed. But I cannot figure out how it got there. I've looked at all the places I can think of where $PATH gets set: .profile, .bashrc, .bash_profile, et cetra but I cannot find who is putting this particular directory into $PATH. Clearly, I do not know all the things that change $PATH when my system starts. Is there a way to debug the startup sequence? Ideally I just want to set a trap on anything that touches $PATH. Or may there is a log file (or ordered set of log files) I can scan. How can I find how each directory gets set in $PATH among all the things that run when my system starts?

like image 419
user749105 Avatar asked Dec 15 '14 15:12

user749105


2 Answers

There are two ways to do this:

  1. Try bash --login -x in a terminal window. This will print every line of the setup scripts as they are executed (kudos go to kojiro).

    Alternatively, you could add set -x near the top of /etc/profile or $HOME/.profile. The danger here is that if you make a mistake, your system may become unusable (if you can't start a new shell anymore) plus you need to be root. If you want to try this, I suggest to create two terminal windows, keep them open at all cost, make the changes and then start new terminal windows to see the effects.

  2. Use grep -r PATTERN / (replace PATTERN with the path that you look for). That should search your whole hard disk for this pattern and can take a long time. Maybe start with your home directory. If you want to search just the . files, use this trick: grep -r PATTERN .??*

like image 168
Aaron Digulla Avatar answered Oct 24 '22 01:10

Aaron Digulla


If you are using BASH, the way the login loading works is this order:

  • /etc/profile is first read in.
  • $HOME/.bash_profile is read in.
  • If $HOME/.bash_profile doesn't exist, then $HOME/.bash_login is read in.
  • If $HOME/.bash_login doesn't exist, then $HOME/.profile is read in.

NOTE: That $HOME/.bashrc is not read in by default. However, many people do source in their .bashrc file at login.

When a shell is invoked, and it's not a login shell, then the $HOME/.bashrc file is loaded.


By default, the Mac's PATH is /bin:/usr/bin:/usr/sbin:/sbin. This is set via /etc/profile which executes /usr/libexec/path_helper. This takes the initial paths from /etc/paths.d/... and from /etc/paths. The /etc/paths.d only adds in the /opt/X11/bin directory if X11 is installed. Otherwise, /etc/paths is used and it contains the default paths.

Mine is set to:

  • /usr/local/bin
  • /usr/bin
  • /bin
  • /usr/sbin
  • /sbin

I use /usr/local/bin for third party tools. or newer versions of already installed tools. For example, /usr/bin/svn is version 1.7.17 while /usr/local/bin/svn is set to version 1.8.10. Me invoking svn will invoke the version of Subversion I installed, and not the native version.

In this case, the 1.8 version of Subversion is actually installed in /opt/subversion/bin/svn, but I create a symbolic link to it in /usr/local/svn. I do this for any tool that I install: The tool gets installed in /opt and I link the binaries and scripts to /usr/local/bin, so they're in my $PATH.

like image 4
David W. Avatar answered Oct 24 '22 01:10

David W.