You’ve probably noticed that when you turn on your computer that some applications start up with it. Some of them are required by the operating system and some you might have installed yourself. Some can make your computer’s fan sound like an airplane taking off.
You can see some of these in the “Login Items” tab of the “Users & Groups” section of your System Preferences.
Other applications have their own login items, which run only when that specific application is opened. We’re going to use Bash’s login items feature to make web development easier.
Why do I need a Bash profile?¶
The Bash profile is a file on your computer that Bash runs every time a new Bash session is created. This is useful because we need to run certain code every time before starting to work.
OS X doesn’t include a Bash profile by default, but if you already have one, it lives in your home directory with the name
.bash_profile. And if you did have one, you probably never saw it because its name starts with a period. The Finder hides folders and files starting with a period to protect casual users from harming the operating system—but because you’re a developer now, we’re going to create a Bash profile!
Creating your Bash profile¶
Open Terminal, copy and paste the following command, and press
$ subl ~/.bash_profile
The tilda (
~) tells Bash to start traversing the file system from your home directory. In my personal case, an equivalent command would have been
subl /Users/Rich/.bash_profile, which means
~ is the equivalent of
~ is a shortcut that makes it generic and usable for everybody to understand and use.
Your Bash profile will pop up in a Sublime Text window.
Understanding your PATH¶
The first edit to your Bash profile is to correct your PATH.
PATH is an environment variable, which simply means that it represents some small bit of data while you use Terminal. Specifically,
PATH contains a list of file system paths where the operating system can find programs to run.
When a developer runs a program in Bash, the operating system will sequentially look for the program in each of the paths that
PATH contains, starting with the first path listed. If the operating system can’t find the program in the first path, it looks for the same program in the second path, and so on, until either eventually finding and running the program or returning an error if the program couldn’t be found.
PATH contains paths that are delimited by a colon (
:). Therefore, the value of
PATH might look something like:
You can see that
/usr/local/bin is the first path, and
/usr/bin is the second path.
/usr/local/bin is where all programs local to your use of the operating system are located. Storing programs for your personal use in
/usr/local/bin is a best practice and highly encouraged. Therefore, the
PATH above is correct.
Likewise, storing programs in
/usr/bin allows programs to be globally accessible by other users. Storing programs globally can sometimes be desirable, but in general it’s discouraged and likely to cause confusion.
Correcting your PATH¶
In versions of OS X prior to Yosemite, Apple mistakenly switched the order of the paths, placing
/usr/bin ahead of
/usr/local/bin, causing much disruption and angst. Apple has since corrected the issue, but it’s still worth changing because doing so won’t harm the computer and will prevent problems from occuring again.
Copy and paste the following into your Bash profile.
# Paths export PATH=/usr/local/bin:$PATH
The first line is a comment, which begins with a hash (
#) and explains surrounding code. The right portion of the second line begins
/usr/local/bin, which is the path we want to prioritize, followed by
:, which joins paths, and finally
$PATH, which evaluates the value of the existing
PATH. By appending
$PATH we can overwrite the original
PATH without destroying its value, making everything nice and tidy!
Next we assign the value
PATH and export it at the same time. Exporting
PATH ensures that the variable is loaded into memory and accessible.
The difference between
PATH is subtle but worth pointing out. When you assign a value to a variable, then the variable should be called without
$. If you want to evaluate the variable to get its value for use in Bash, prepend
$ to the variable name.
Save and close the file.
Sourcing your Bash profile¶
We edited our Bash profile, but it is critical to remember the code in Bash profile runs only when a new Bash session is created, which is called sourcing. Therefore, our changes will take effect when you quit Terminal and open it again to make sure that
PATH is in fact exported. When Terminal is open again, you can check the value of
PATH by running the
$ echo $PATH
The output might look like one of the two:
Again ensure that
/usr/local/bin is listed prior to
Although Terminal allows a Bash profile to be sourced on command without restarting (
source ~/.bash_profile) the method can be unreliable.
PATH is just one evironment variable we changed in our Bash profile. There are a lot more, like
USER for the current user (you!) and
HOME for the path to the home directory of the current user. We will edit our Bash profile a few more times to run other important code in the future.