Professional Documents
Culture Documents
What Exactly Is Your Shell PATH - I Was Recently Attempting To Explain To - by Jalen Davenport - Medium
What Exactly Is Your Shell PATH - I Was Recently Attempting To Explain To - by Jalen Davenport - Medium
https://medium.com/@jalendport/what-exactly-is-your-shell-path-2f076f02deb4 1/13
1/7/2021 What Exactly is Your Shell PATH?. I was recently attempting to explain to… | by Jalen Davenport | Medium
I was recently attempting to explain to a friend what the PATH variable on his
computer is used for. After writing up a nice long message in the Slack group we
were conversing in, I realized it was good material for a blog post. So here
goes…
Y
ou’ve probably heard people and/or blog posts talking about editing
your PATH or “such-and-such” not being present in your PATH. And
you’re just like, “Wait, what exactly is the PATH?”. Good question!
If that definition doesn’t make a whole lot of sense at this point, read on and
I’ll explain how it all works in-depth…
https://medium.com/@jalendport/what-exactly-is-your-shell-path-2f076f02deb4 2/13
1/7/2021 What Exactly is Your Shell PATH?. I was recently attempting to explain to… | by Jalen Davenport | Medium
your PATH is pretty much a bunch of directional signs telling your computer where to go
https://medium.com/@jalendport/what-exactly-is-your-shell-path-2f076f02deb4 3/13
1/7/2021 What Exactly is Your Shell PATH?. I was recently attempting to explain to… | by Jalen Davenport | Medium
rm , etc)
https://medium.com/@jalendport/what-exactly-is-your-shell-path-2f076f02deb4 4/13
1/7/2021 What Exactly is Your Shell PATH?. I was recently attempting to explain to… | by Jalen Davenport | Medium
popular package manager for macOS, puts any executable files it installs in
/usr/local/Cellar (and those executable files are then symlinked into your
/usr/local/bin folder for ease-of-use). Composer, a dependency manager
for PHP, installs executable files in ~/.composer/vendor/bin . Depending on
how you install MYSQL, some of its utilities ( mysqldump for example) are
installed in /usr/local/mysql/bin .
But do you really want to have to type out the full path to your executable
each time? Would you not rather be able to run $ ls or $ my-program ?
That’s where PATH comes in! Here’s an analogy of how the PATH works:
imagine you’re a manager of several large warehouses. In each of these
warehouses you’ve got a bunch of different products. You also have a
spreadsheet or a map that lists out the location of each warehouse and what
products are being stored at each one.
PATH works in a similar way — it’s a global variable that contains a string of
different paths separated by a : . When you type the name of an program
without using the absolute path, your computer then uses this variable to
understand what directories it should look in to find the executable you’re
requesting. Starting with the first directory in the list, it will search for a
match to what you requested — which means that directories that comes
first in the PATH take precedence over directories later on in the list.
https://medium.com/@jalendport/what-exactly-is-your-shell-path-2f076f02deb4 6/13
🔥 Tip: If you want to see what your path is currently set to, run
1/7/2021 What Exactly is Your Shell PATH?. I was recently attempting to explain to… | by Jalen Davenport | Medium
$ echo
$PATH .
I popped open a Mac with a fresh install of macOS and on that machine the
“default” PATH seems to be /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin .
https://medium.com/@jalendport/what-exactly-is-your-shell-path-2f076f02deb4 7/13
1/7/2021 What Exactly is Your Shell PATH?. I was recently attempting to explain to… | by Jalen Davenport | Medium
So out of the box, your Mac should automatically use the PATH to fulfill
requests for any programs installed in the default/standard bin locations.
But what if you’re using another tool like Composer or Homebrew and want
to add other directories to your PATH?
For example, let’s say we want to add a new directory that takes precedence
over the currently added directories (i.e. we want to add it in front of the
others), we can run the following in our Terminal:
$ export PATH="/my/directory/bin:$PATH"
Now when we run $ echo $PATH you should see your new directory listed
before the other directories in the PATH (something like
/my/directory/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin ).
https://medium.com/@jalendport/what-exactly-is-your-shell-path-2f076f02deb4 8/13
1/7/2021 What Exactly is Your Shell PATH?. I was recently attempting to explain to… | by Jalen Davenport | Medium
But maybe you want to add this directory to the end of the PATH so it is the
last directory to be searched. Then you could do something like this:
$ export PATH="$PATH:/my/directory/bin"
If we edit the /etc/paths file, the PATH changes will be applied globally
across all the users on the system. If you open /etc/paths in a text editor,
you’ll notice there are a bunch of directories listed on individual lines. To
add your custom directory, create a new line anywhere in the file and add
your directory path (directories higher up in the file take precedence over
https://medium.com/@jalendport/what-exactly-is-your-shell-path-2f076f02deb4 9/13
1/7/2021 What Exactly is Your Shell PATH?. I was recently attempting to explain to… | by Jalen Davenport | Medium
If you’re using Bash (the default shell on most UNIX systems), you’re
probably going to want to add this to either your ~/.bash_profile or your
~/.bashrc file. If you’re using another shell, you can add it to whatever
file(s) that shell uses (ZSH for example uses a ~/.zshrc file). Basically, all
you need to do is just open the appropriate file in TextEdit or a code editor
and somewhere in the file add the custom PATH declaration you need. For
example, my ~/.bashrc contains the following lines:
export PATH="/usr/local/bin:/usr/local/sbin:$PATH"
export PATH="$PATH:~/.composer/vendor/bin"
After you’ve added the necessary exports, save the file and quit and
relaunch Terminal to load the new changes.
https://medium.com/@jalendport/what-exactly-is-your-shell-path-2f076f02deb4 10/13
1/7/2021 What Exactly is Your Shell PATH?. I was recently attempting to explain to… | by Jalen Davenport | Medium
A PRACTICAL EXAMPLE
A common PATH-related issue I hear of all the time is certain applications
or tools complaining about not being able to find the mysqldump executable
(a database backup utility that is installed with MYSQL). And because it
can’t find this executable, it’s getting hung up on whatever task it’s trying to
run and spitting out error codes at you. Like I mentioned earlier, MYSQL
has it’s own /bin -like directory where utilities like mysqldump are installed.
So what we want to do is add the path to this MYSQL /bin folder to our
PATH variable so this tool we’re trying to use will be able to access
mysqldump to complete the task. We’re going to opt for editing our shell files
because we don’t have a need at the moment to apply this PATH change
globally to all the users on the computer. Also, we’re using BASH on this
particular computer.
https://medium.com/@jalendport/what-exactly-is-your-shell-path-2f076f02deb4 11/13
1/7/2021 What Exactly is Your Shell PATH?. I was recently attempting to explain to… | by Jalen Davenport | Medium
Let’s go ahead and open ~/.bash-profile in a text editor and add the
following line:
export PATH="$PATH:/usr/local/mysql/bin"
Next, we’ll need to save the file, close our text editor, and then quit and
relaunch Terminal so the new PATH will take effect.
Now we should be able to re-run the task we were trying to complete earlier
and it should be able to find mysqldump in the PATH.
Hopefully now you’ve got a pretty good grasp of what your PATH is and how
it works!
If you’re having problems with any of the above or still trying to wrap your
head around how PATH works, please reach out to me and I’ll try to help
you out. You can leave a comment below or reach me on Twitter
(@jalendport).
https://medium.com/@jalendport/what-exactly-is-your-shell-path-2f076f02deb4 12/13
1/7/2021 What Exactly is Your Shell PATH?. I was recently attempting to explain to… | by Jalen Davenport | Medium
If you found this post helpful or enjoyed the read, please follow me here or
on Twitter to see future posts!
https://medium.com/@jalendport/what-exactly-is-your-shell-path-2f076f02deb4 13/13