76

I have been looking throughout the system but I cannot find it. When I do echo $PATH I get the stuff I added, plus the default path. I do not have a .profile, and I do have a .bashrc, but the default path is not in there. I am looking for it just to know where it is located because all the tutorials explain that its in .profile... but what if you don't have one? Where is it located then? Anybody have any ideas?

Andy
  • 10,553
  • 21
  • 75
  • 125

7 Answers7

121

If you do sudo man path_helper, it talks a bit about how it puts the path together. You might look in /etc/paths and /etc/paths.d. I did, and found what I was looking for.

beOn
  • 1,799
  • 1
  • 12
  • 13
  • `man path_helper` gives me `No manual entry for path_helper`, but editing `/etc/paths` worked. – Koen. May 10 '14 at 11:28
  • @Koen., try `sudo man path_helper`. I'll amend the entry. Thanks for the note! – beOn May 13 '14 at 16:16
  • Thanks a million for this. This tip helped me resolve an annoying issue of diverging `$PATH` configurations between shell and Emacs on OSX, see [problem post](http://emacs.stackexchange.com/questions/10722/emacs-and-command-line-path-disagreements-causing-issues-with-haskell-cabal/10735#10735). – Galder Zamarreño Apr 17 '15 at 07:18
  • 3
    There's a ton of stuff in my path that's not set up by .zshenv or anything in /etc/paths.d (there is no /etc/paths). So now what? – Oscar Aug 03 '21 at 22:20
  • This answer lists some other possibilities: https://superuser.com/a/1464744 – beOn Apr 26 '22 at 21:41
18

Many system-wide settings including PATH are set in /etc/profile which is read in by bash at startup. On Mac OS X this file usually uses path_helper to set PATH. This utility in turn reads the information from other system configuration files under /etc (see path_helper manpage).

Note that even if you disable the reading of the initialization files by bash (e.g. with command-line options like --noprofile) it will still inherit the environment of the parent process.

Adam Zalcman
  • 26,643
  • 4
  • 71
  • 92
  • I am only getting regex. I am looking for the stuff like /usr/local/mysql/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin But I don't have the .profile with that, and its not in my .bashrc, but I know it exists cuz I can echo it from $PATH – Andy Mar 23 '12 at 01:03
17

If you start at /etc/profile, it should look something like this:

if [ -x /usr/libexec/path_helper ]; then
    eval `/usr/libexec/path_helper -s`
fi

It's testing to see if the file exists and if so, executes it. If you execute it by hand, you'll get something like this:

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

I believe that's what you're looking for. So it comes from /etc/profile, which in turn calls an executable that sets the path.

Mike Taber
  • 833
  • 6
  • 21
  • 1
    I understand that, and I looked at what Adam pointed out, but in order for that to happen, it needs to get the info from somewhere. Do the expressions inside path_helper actually generate /usr/local/mysql/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin ? – Andy Mar 23 '12 at 01:11
  • 4
    That is indeed where the paths come from (via `/etc/profile` and `/etc/csh.login` depending on whether your shell is sh/bash or csh/tcsh). Look at the code inside the script, though, to get the rest of the answer: it looks at `/etc/paths` and `/etc/paths.d/*` (all of which should be files, with one line per path-entry to include). – torek Mar 23 '12 at 05:48
  • 1
    I don't understand what you mean by the "expressions". The path_helper is a binary executable. It's being called as part of a shell command using the backticks (line 2) which outputs the path helper which is generated. The eval part of the line then indicates that the output of path_helper should be executed as part of the shell. Note the last part of it is export PATH;, which puts the results into your environment shell. – Mike Taber Mar 26 '12 at 03:31
  • Got an update on this question and looked through. I didn't know it at the time @MikeTaber , but I'm a huge fan. Stanning right now at what a small world it is.Hope you're doing well. Completely out of scope comment haha. – Andy Jan 30 '22 at 18:00
  • Thanks Andy! Yes, it's a small world, for sure. I answered this way back when I was still doing Unix shell scripting and working on a product that needed that. Hope it was helpful. – Mike Taber Jan 31 '22 at 18:10
7

As mentioned in the accepted answer, the $PATH is built by first looking into the content of the file /etc/paths, and then looking into every file in the folder /etc/paths.d. So, the $PATH in the pre-installed bash system installation contains every entry in these files, as well as in other shell types.

However, because in the latest Mac OS versions the default shell is zsh, I followed a couple of tutorials in which the writer avoided to change the $PATH for the bash shell, and simply added new entries to the $PATH by editing ~/.zshrc the following way:

export PATH=/path/available/only/for/zsh/shells:$PATH

The above command adds /path/available/only/for/zsh/shells to the $PATH, and the added path will only be available in zsh shells.

I hope this helps someone who, like me, had too many entries in the $PATH in zsh shells, and couldn't figure out where they were coming from!

ccoutinho
  • 3,308
  • 5
  • 39
  • 47
1

The .profile file on Mac is located in your user folder: ~/Users/youruser/ However, the .profile file is hidden. You can press Command+shift+. (command, shift, dot) while on Finder to see them.

  • 1
    I'm on Catalina and don't have the .profile file at that location. Or a .bash_profile or .bashrc. – Nic Foster Sep 22 '20 at 02:16
  • @NicFoster: Recall that in Catalina, Apple decided that `zsh` would be the default shell - replacing `bash`. You may (or may not) have files `~/.zprofile` and `~/.zshrc` which serve the same purpose. –  Oct 01 '20 at 21:20
  • I'm not sure, but it looks like Catalina doesn't give you direct access to $PATH. Instead, look at /etc/paths. For me, line 1 is /usr/local/bin; line 2 is /usr/bin, and so on. Here's how I interpret it: each line has one path on it, and $PATH is the concatenation of each of those lines. Want your path to be first, make it the first line. Want it last,put it on the last line. – Gregg Williams Mar 27 '21 at 02:45
  • This doesn't really answer the question. The location of the default path is given in the accepted answer by beOn. – Jan Hettich Sep 18 '21 at 22:06
0

There's one important fact I only realized today while debugging a problem: the profile settings (.bash_profile etc.) are only read by login shells. They are not read by the processes that are used to launch your applications.

You launch your applications in diverse ways: click the icon in /Applications, or type the name in Spotlight search, or click an icon in the Dock ... In all those cases, the application itself (i.e the binary or shell script inside the application) is launched by launchd without any parent shell. Meaning that your profile is not run and that your custom settings (PATH, environment variables ...) will be ignored.

That can cause all sorts of trouble, for example if you setup you environment to use a specific version of Java: your application will not see that and use the "default" java, which will be the one with the highest version number.

In my case, the problem is that my application was crashing when run via the application launcher, but runs fine when run from a terminal window ... The reason was that I had a custom path that included some libraries required by the application, but that was not available when the application was run by the launcher.

The solution I used was to symlink the libraries needed into /usr/local/lib

Albert Godfrind
  • 1,978
  • 1
  • 12
  • 20
0

I'm posting my scenario here in case anyone else ends up here with slap-dash bash skills. I'm running MacOS Ventura and have Anaconda installed which is also initialized in the terminal. I wanted to edit the $PATH variable in an editor somewhere since I wanted to put one directory first and then later remove it. Where to do this was very confusing since there are multiple areas that deal with your $PATH variable according to Google searching. If you're in my situation or are just using Anaconda then here is where you can edit path.

Since I'm on Mac I have the .zshrc file. Open that and you should see some lines towards the bottom dealing with anaconda. Specifically you'll see # >>> conda initialize >>>. Below that is Anaconda setting its own directories to the front of $Path. You'll notice the else: statement appending the /anaconda3/bin: path recursively PATH="/somedirectorypath/anaconda3/bin:$PATH". If you want to put a path in front for the moment you can add export PATH="newpath:$PATH" after the anaconda block. Then you can go about your business and remove it. Or you can change the path completely by PATH = "newpath1:newpath2:newpath3".