Something adds an undesired path to my PATH environment variable.

How can I find the culprit?

I've already looked at:

  • ~/.bashrc
  • ~/.bash_profile
  • /etc/bashrc
  • /etc/profile
  • /etc/environment
  • Have you looked at the files in /etc/profile.d? Is there any other software on the system that edits environment variables such as this: github.com/cea-hpc/modules? Oct 31, 2019 at 0:56
  • Yes, nothing... It's basically PATHs of applications I've installed using yum/rpm.
    – Alexis
    Oct 31, 2019 at 6:42

3 Answers 3


This maybe an overkill, and you should (of course) attempt to find it by tracing the order of running scripts, but there are times when that tree becomes way to difficult to parse. So it is possible to simply grep for it:

grep --regexp='PATH=.*/path/that/you/try/to/find' --binary-files=without-match -r /

Please note, that this is a very long process, and your harddrive will be going wild throughout (you are basically reading every text file that you have).

  • I've tried it already but get grep: memory exhausted. That's funny bash/terminal/terminator isn't able to list all scripts it loads.
    – Alexis
    Oct 31, 2019 at 6:43
  • can you make sure that you exclude binary (as shown above) and also change * to \{0,100\} in regex?
    – v010dya
    Oct 31, 2019 at 7:19


grep -rF --binary-files=without-match ':/the/undesired/path' / 2> /dev/null

If /the/undesired/path is $UNKNOWN_VARIABLE_NAME_FOR_THE_UNDESIRED_PATH or , this will obviously not work.

Note that /home/user/undesired/path will often be assigned as $HOME/undesired/path.

Run the same without : to also get variable assignments:

grep -rF --binary-files=without-match '/the/undesired/path' / 2> /dev/null

If that doesn't work, find all PATH assignments and filter manually:

grep -rwF --binary-files=without-match 'PATH=' / 2> /dev/null

Separate file finding and searching with find and xargs, see man pages for each.

sudo find / -type f -print0 | \
   xargs -0 --no-run-if-empty \
       grep -l 'PATH'

to list all filenames that mention PATH. Append

| xargs less

to look at the files.

One can't search for /some/deep/path/bin, because it could be expressed as $SDP/bin, for all we know.

  • That gives thousands of files.
    – Alexis
    Nov 1, 2019 at 4:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .