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.