You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Correct method to set my PATH on Mojave and later 2020

I find a lot of old and new ideas on the proper way to set up your path on macOS. Some ancient and most likely not relevant today.


I have exported some paths and they are in .bash_profile


When I:

source .bash_profile

I end up with duplicates in my path (how to prevent this?)


To reset my path I have tried:

eval `/usr/libexec/path_helper -s`

Which works well because it pulls any paths from paths.d but for some reason puts the paths I have in .bash_profile at the end instead of the beginning of my path (is this avoidable?)


If I "source .bash_profile" again I end up with duplicates in my path so I attempted to set the system's default path at the beginning of my user's .bash_profile using:

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

But this leaves out the paths in paths.d (if there was a way to add those paths in paths. d here I would be set)


I would like to get the default paths (I think these are Mojave and later Paths with the addition of /usr/local/bin)

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


Including whatever is in paths.d which right now is:

/Applications/Wireshark.app/Contents/MacOS


And add any custom paths in user's .bash_profile. I would like those all to stay in order and put even if I run source .bash_profile.


This site says this:

Conclusion
Use $HOME/.bash_profile file when you need to generate the PATH variable for a single user account.
Use /etc/paths.d/ directory via the path_helper tool to generate the PATH variable for all user accounts on the system. This method only works on OS X Leopard and higher.

Is this advice current?


Is the following correct on prefixing your path and appending it?


If you use this syntax:

export PATH=/usr/local/opt/nano/bin:"$PATH" #appends to path instead of prefixing


Whereas:

export PATH=$PATH:/usr/local/opt/nano/bin  #prefixes to path instead of appending


And, If the idea is to use paths.d for system wide additions to PATH then the concept of using .profile for exporting paths is deprecated?


Also, I thought that exporting paths in .bash_profile caused them to take precedence. Yet when resetting paths using eval `/usr/libexec/path_helper -s` it puts my .bash_profile paths at the end?!


Hope someone can enlighten me. Thanks!

iMac 27″, macOS 10.14

Posted on Aug 30, 2020 3:56 PM

Reply
Question marked as Top-ranking reply

Posted on Sep 3, 2020 2:46 PM

A few things...


You shouldn’t manually source .bash_profile. It is loaded automatically. And you should probably switch to zsh anyway. Apple hasn’t updated bash in years because it is GPL licensed. The zsh she’ll is very similar to bash. You won’t miss it.


The path_helper system is only useful for a machine with multiple user accounts that need to run the same Terminal commands. That is extraordinarily rare. I suggest you just ignore it and use dotfiles instead.

Similar questions

4 replies
Question marked as Top-ranking reply

Sep 3, 2020 2:46 PM in response to johnnyjackhammer

A few things...


You shouldn’t manually source .bash_profile. It is loaded automatically. And you should probably switch to zsh anyway. Apple hasn’t updated bash in years because it is GPL licensed. The zsh she’ll is very similar to bash. You won’t miss it.


The path_helper system is only useful for a machine with multiple user accounts that need to run the same Terminal commands. That is extraordinarily rare. I suggest you just ignore it and use dotfiles instead.

Sep 3, 2020 2:31 PM in response to BDAqua

I responded before I even tried the examples. When I use that line at the beginning of my .bash_profile and I have other paths exported in that same .bash_profile I still get duplicates when I use source .bash_profile.


I thought the intention of having that one line in my .bash_profile was to reset the path to defaults whenever I sourced it, and then add any other paths I have (on a per user basis) inside .bash_profile. So that using the source command would always give me a clean path by adding all three together:


  • etc/paths
  • etc/paths.d
  • and any paths inside .bash_profile (either appending or prepending to path)

Sep 3, 2020 12:57 PM in response to BDAqua

@BDAqua I really can't thank you enough for finding that needle in that haystack.


Does Apple want us to add system wide paths to paths.d instead of adding them to .bash_profile


This is especially helpful:


Using path_helper

macOS also includes a helper to assistance in the management of your $PATH. It's located here /usr/libexec/path_helper.

So instead of manually crafting your base $PATH as mentioned above you can instead use this snippet to get a known good starting point for your $PATH.

[ -x /usr/libexec/path_helper ] && eval $(/usr/libexec/path_helper -s)

This will take care to initialize $PATH so any directories listed in /etc/paths and /etc/paths.d/get added automatically.


This note in man path_helper says we should add the above line inside .bash_profile. Perfect.

NOTE

     The path_helper utility should not be invoked directly.  It is intended only for use by the shell profile.

Correct method to set my PATH on Mojave and later 2020

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.