Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I recently started configuring a OSX 10.7 environment for development. I've installed HomeBrew, and added the following to my .bash_profile

export PATH="/usr/local/bin:/usr/local/sbin:~/bin:$PATH"        

Everything is working great except when I echo $PATH i get the following string of duplicates: /usr/local/bin:/usr/local/sbin:~/bin:/usr/local/bin:/usr/local/sbin:~/bin:/usr/local/git/bin:usr/local/bin:/usr/local/git/bin:usr/local/bin:/usr/local/git/bin:usr/local/bin:/usr/local/git/bin:usr/local/bin:/usr/local/git/bin:usr/local/bin:/usr/local/git/bin:usr/local/bin:/usr/local/git/bin:usr/local/bin:/usr/local/git/bin:/usr/local/git/bin:/usr/local/git/bin:/usr/local/git/bin:/usr/local/git/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

I don't think I've made any changes to path besides the single line in my .bash_profile. What is the best way for me to pair down those duplicates? Is there a way to hunt down which files are modifying my path and try to eliminate them?

Thanks!

share|improve this question
    
Anything in /etc/paths.d/? Any other bash configuration files? –  slhck Jul 16 '12 at 17:53
    
I really wouldn't worry about it. Bash will only do a single lookup for a command name once during a session; afterwards, it stores the command's full path in an internal hash table. You will probably spend far more time looking for the source of the duplicates than bash will ever, cumulatively, spend searching for the location of executables in PATH directories. –  chepner Jul 16 '12 at 18:06
    
@slhck /etc/paths.d has one file with the line /usr/X11/bin in it. I haven't made any other bash configuration files that I know of (I only have .bash_profile in ~) –  Nick Tomlin Jul 16 '12 at 18:20
add comment

3 Answers

up vote 1 down vote accepted

Having made my comment, here are some suggestions for pruning $PATH anyway.

Looking at your path, it appears that .bash_profile is being executed twice, or the modification made in .bash_profile is duplicated elsewhere.

There are 7 duplicate additions of '/usr/local/git/bin' and 'use/local/bin' [sic], followed by 5 more copies of '/usr/local/git/bin'. Depending on how git is installed, you may be able to query your package manager about what files were installed with git; there could be some configuration files or modified system files that affect PATH.

Put set -x at the very top of your .bash_login, then start a new login shell. You should get a lot of output that shows exactly what bash is doing on startup, which should help you figure out where PATH is being modified. You can remove set -x once you figure it out or give up. If you don't find anything, you could also add it to the beginning of /etc/profile to trace what the system does before your own .bash_profile is processed.

share|improve this answer
    
+1 for set -x –  user3463 Jul 16 '12 at 20:15
    
I used set -x, saw that the only major modification to PATH was in my .bash_profile and removed that line. Now my path is much cleaner (don't know how that line managed to wreak so much havoc). Thanks so much for the answer @chepner! –  Nick Tomlin Jul 18 '12 at 19:08
add comment

I added this to my $HOME/.bashrc (you should also be able to add it to .bash_profile instead if you wish) to remove duplicate entries from the $PATH. I've only tested on linux but should also work on mac. It should be added after the initial export PATH.

export PATH=$(echo $PATH | awk -F: '
{ for (i = 1; i <= NF; i++) arr[$i]; }
END { for (i in arr) printf "%s:" , i; printf "\n"; } ')
share|improve this answer
add comment

An installation of git modified your .bash_profile without serious preliminary check. This installer corrupted it (apparently 12 times). Use the receipe suggested by rash, but just once to define the correct minimal PATH to include back in your .bash_profile.

You don't need to initialize your PATH in your .bashrc. This initialisation script is reserved to configuration which have to change with each level of shell, for example a prompt, a position, a color, but not a PATH.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.