19

What is the PATH in a Mac OS? I have used it to install global command-line tools, but the only documentation I have found are tutorials for doing just that, with no real explanation of what is happening under the hood. I also haven't found a relevant page on Wikipedia.

So what is the purpose of PATH and what is the difference between /etc/paths and ~/.bash_profile**?

For example, in my paths file, I see this:

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

But in my ~/.bash_profile, I see this:

# Setting PATH for Python 2.7 # The orginal version is saved in .bash_profile.pysave PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}" export PATH 

Is this just a user-specific addition to the paths file? What's all that stuff in paths, like /usr/bin?

2
  • 2
    Please don't combine multiple questions in a single post. Each of your questions would be welcome here separately but not all together like this. Please trim this down to a single question, you could combine 1 and 2 but the others have nothing to do with PATH specifically. 3 is answered here 4 is explained here and 5 does not make much sense I'm afraid. Nor does your bonus question, what files are binary? Scripts are not. Commented Jan 29, 2014 at 22:36
  • I have edited my question down. Also, I found those links helpful. Thanks. Commented Jan 29, 2014 at 22:41

2 Answers 2

19

1. What is the purpose of PATH?

PATH is an environment variable that contains a colon-separated list of directories where your shell will look for executables that you name on the command line without providing an explicit path to them (as in your jsdoc example). Thus, if your PATH has

/usr/bin:/bin:/home/bin 

then any executable you call by name will be searched for in these directories (in that order) and the first executable found is the one executed.

2. What is the difference between /etc/paths and ~/.bash_profile

According to this question on ServerFault, /etc/paths is used to set PATH globally (i.e. system-wide, for all users) while ~/.bash_profile is used to set per-user preferences (where ~ will be the user's home directory). What is written in .bash_profile can either add to the global PATH from /etc/paths or override it completely.

For the record, /etc/paths seems to be a MAC OS peculiarity: I haven't come across it on GNU/Linux, at least.

5
  • I just edited them down, but thanks for answering all of them! Commented Jan 29, 2014 at 22:42
  • @ggundersen I trimmed my answer to fit your edited question. Commented Jan 29, 2014 at 22:42
  • @ggundersen you might also want to read my answer and the links therein about /etc/paths on OSX. Commented Jan 29, 2014 at 22:43
  • Thank you for the answer! Could you please tell how/where can one learn more about these topics systematically? Commented Jan 17, 2022 at 12:35
  • I want to ask this long time ago, why they don't return the line for each variables in PATH for readability? ` /usr/bin -> /bin -> /home/bin ` Commented Mar 2, 2022 at 8:31
10

What is the purpose of PATH?

It is how your shell finds programs. When you type ls, for example, that is running a program called ls which lives in /bin on most systems, including Mac OS X. Your shell can only find that because /bin is in the PATH.

The shell searches for programs in PATH order, left-to-right. If there are two ls programs on your system, and their directories are both in your PATH, it will find the one in the directory listed first in your PATH.

What is the difference between /etc/paths and ~/.bash_profile

Paths set in /etc/paths are added to the PATH at startup in all shells on Mac OS X.

~/.bash_profile is just one of several shell scripts run by Bash at startup, and several of these are Bash-specific. Therefore, if you change your shell — Mac OS X also ships tcsh and zsh, for example — changes made to /etc/bashrc don't apply to your new shell.

Also, /etc/paths is a system-level configuration file, while ~/.bash_profile is per-user. If you had multiple interactive users set up on your Mac, you wouldn't want to add directories to /etc/paths that you didn't want to appear in everyone's PATH. The same is true for /etc/bashrc, except that of course that only applies to users that use Bash as their shell.

What is the difference between PATH and $PATH and ${PATH}?

You usually only see the variable without the sigil when you are setting the variable. FOO=bar sets the FOO environment variable to the string bar. If you type set, the shell shows you all environment variables, and in that case you also see the variable without any sigil.

$PATH and ${PATH} are usually interchangeable. They tell the shell to expand the current PATH variable's value in-place. The difference has to do with how and where you use them. For example:

$ FOO=bar $ echo $FOO bar $ echo $FOOx $ echo ${FOO}x barx 

In the second echo command, the shell prints nothing because it asks the shell to print a variable called FOOx, which doesn't exist; the shell treats nonexistent variables as empty. Because the third echo uses the curly brace syntax, it allows the shell to see that you are requesting the FOO variable, and that the x is just another character you want printed immediately afterward.

There's another way to get the same effect without curly braces, by the way:

$ echo "$FOO"x barx 

Environment variables are expanded in double-quotes, and the quotes separate the x from the variable expansion so the shell does the right thing.

I'm using the FOO environment variable here just for clarity. Everything I've written above applies just as well to PATH, since it is just another environment variable. The example texts would just be a lot longer.

What does export PATH mean? When do we export?

Taking the above FOO example, if you were to run another program, it wouldn't see the FOO variable because it is not exported. It lives only within that one shell instance:

$ echo $FOO bar $ bash $ echo $FOO $ exit $ export FOO $ bash $ echo $FOO bar 

When I first run a new bash shell under the one I was already using, then try to show the value of FOO, I get a blank because FOO wasn't exported for sub-programs to see. Then I exited the second bash instance, exported it, ran bash again, and now the second instance sees the value of FOO.

You use export when you want sub-programs to see values set in the shell, and don't use it when you don't want that to happen.

Typically, I don't export temporary variables in shell scripts, because I don't want them to change how programs run from that shell script behave.

I'm not going to answer any of your other questions. You're only supposed to ask one question at a time. I've only answered this many because they're vaguely related. Everything else belongs in a separate question.

1
  • Thank you for the answer! Could you please tell how/where can one learn more about these topics systematically? Commented Jan 17, 2022 at 12:35

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.