Unfortunately, this post isn’t about how you can set your sights to the future. Instead, we focus on a thing called the PATH variable, where it stands, and how you can modify it to access programs in other places.
The PATH variable is a list of directories which contain programs to work with. When using the terminal (or command line for you people on Windows), you’ve accessed the PATH variable in order to reach the programs you’re after without knowing it.
It is a variable
The reason it’s called the PATH variable is that it can be changed, and that’s we’ll focus on in this post. There’s a few differences here and there between operating systems, so I’ll point them out as we see them.
One important thing to remember is that the order of directories matters in how the system searches the directories for programs to run—the earlier a directory is in the list, the higher it is prioritised. This means if you insert a directory in front, you can end up replacing programs normally provided by the system.
In Windows 10, you can get to the PATH variable by going through the Control Panel, into System, and Advanced System Settings. The button is then visible under the System Properties window:
Opening up the Environment Variables window itself throws a bit at you, but the important thing to focus on are the User variables above:
You can see that the PATH variable has a series of directory paths, separated by a semicolon. Going to edit it opens up a new window which shows everything off nicely:
This isn’t present in earlier versions of Windows, so you’ll have to type it in yourself by double clicking the value of the PATH variable.
If you want to add to the front of the PATH, type the full directory path (or paste it in), and add a semicolon to separate it from the next directory.
If you want to add it to the end of the PATH, add a semicolon to the end, then paste your directory path in.
For Windows 10 users, just click the New button, then Browse, then navigate using the folder controls and clicking OK. It will then appear in the list.
UNIX style/Linux style
If you’re a Linux user, a macOS user, or maybe BSD, you don’t get a system dialogue like Windows users. But don’t worry, because it’s much easier to alter the PATH variable. It might look a little messy, but so long as you understand how it works, you’ll be fine.
To get going, you’ll want to open up your preferred terminal application, and type
echo $PATH and execute, you’ll probably get something like, but not precisely this (lines have been separated for readability):
$ echo $PATH /home/minh/.gem/ruby/2.5.0/bin: /home/minh/.cargo/bin: /home/minh/.npm-global/bin: /usr/local/sbin: /usr/local/bin: /usr/bin: /opt/android-sdk/platform-tools: /usr/lib/jvm/default/bin: /usr/bin/site_perl: /usr/bin/vendor_perl: /usr/bin/core_perl
As stated earlier, the PATH variable will be searched from start to end when you intend to execute a program.
So how about adding some directories to that list? It’s easy, just edit the proper file! If you’re using the defaults, chances are the file to edit is
~/.bashrc. If you’re me with Zsh, the file you want to edit is
~/.zshrc. It’ll be similar if you’re using anything else!
The reason for the
~ is because these configuration files are based in your home directory, files which you can tailor to your needs.
Anyway, to edit the variable, you’ll want to open up this file in a text editor. We’ll use nano to open it up, with
nano ~/.bashrc. You’ll likely get a little bit of preconfigured stuff, though we’ll leave it for now. Navigating to the bottom of the file, we can make our new changes.
For instance, you get a program that's cool to you. It's simply called cool, and it exists in
If you want to add this directory to the front of the PATH, you can do it by specifying the directory before listing the variable itself, separated by a colon (:).
What does that look like? It looks something like this:
This can be useful in context of how the PATH variable is processed. You can place it at the front if you want to use your version of
cool over the version provided in
If you want to add it to the end of the PATH, simply add it to the end instead:
What’s important is that
$PATH is present, as it encompasses all those other directories that are referenced, including the system binaries. If you don’t include it, you can lose access to system utilities like
rm to name a few.
You can save the file by hitting
Y to confirm, then
X to exit.
To confirm that your changes worked, you can simply open up another terminal, and running
echo $PATH again. You should see it in the output at the front or end respectively.
Alternatively, you can refresh your existing terminal by running the
source command. Simply type
source ~/.bashrc to reload the configuration, and you should be able to confirm that it works by following the method above!
I hope this helps you developers in understanding PATH variables, and I’ll see you next time!