Doesn’t matter if you are a developer or not, you are bound to deal with a PATH issue at least once.

What is PATH?

PATH is an environment variable which decides what commands you can or cannot run on your PC.

The commands you run on the CLI are, >95% of the times, binaries — executable files compiled with a compiler (.exe on Windows). mv, touch,chown, chmod in Linux are all normal programs. But you do not have to know where they are stored in order to run them. Just type in the name and the program runs. Or as we say, the command completes. That’s because your OS has pre-configured your PATH with their locations.

What if there’s no PATH?

You can absolutely run comamnds without a properly configured PATH. You can either always cd to the location your executables are stored in and then run them with the current directory reference (e.g. ./touch newfile) or you can always supply the complete path to the executable to run it from anywhere (e.g. /usr/bin/touch newfile).

How does PATH look like?

The PATH variable holds a list of directory locations separated by a colon (:) on *—Nix systems and a semi-colon (;) on Windows systems. When you run a command, the name is searched through all the directories listed in the PATH starting from the leftmost entry to find a matching file to execute.

It means there are two cases when running a command can error out:

  1. If you rename the corresponding program/binary/executable.
  2. If you remove the directory path holding the corresponding program/binary/executable from the PATH.

To illustrate the point, you can try this experiment.

  1. Fire up your Linux box or WSL if you are on Windows.
  2. Head over to the terminal and type in which touch. If that doesn’t work, try whereis touch. You’ll get touch’s location printed.
    screenshot of the terminal

    Screenshot of the Terminal

    • You can verify if the directory reported in is in the PATH or not with echo $PATH.
  3. Now rename that file.
    sudo mv /usr/bin/touch /usr/bin/touchmenot
  4. Try running the touch command now. It fails. Try running touchmenot. Your touch is now touchmenot.
  5. Revert the change to make things go back to normal again.
    sudo mv /usr/bin/touchmenot /usr/bin/touch

It should be clear now how important the PATH is.

Next time you download and install something but the command that “should work” doesn’t work, you know it’s your PATH to be blamed.

Setting PATH

On Windows

Assumption made:

  • you have a MinGW-w64 build extracted at the root of the C: drive
  • you want to have the mingw utilities accessible anywhere from the command line
  1. Hit the Windows Key and Search for “environment variables”. Open the first result.
    screenshot of search result

    Search Result to Choose

  2. Choose “Environment Variables” in the opened “System Properties” window.
    screenshot of the system properties window

    System Properties

  3. Based on whether you want the utilities available to all the user accounts on the PC or just yours, choose the PATH under “System Variables” or “User Variables” section respectively and hit “Edit”.
    screenshot of variables window

    PATH under User Variables

  4. Select “Browse” in the next window and locate the bin folder of the build just like you would do in the File Explorer.
    • You can also just copy-paste the full path from the File Explorer by clicking “New”.
      screenshot of path editing window
      screenshot of folder browsing window
  5. There should now be a new entry in the editing window.
    screenshot showing the new entry in the editing window

Congratulations! You just added a new directory in your PATH.

How to choose between “User Variables” and “System Variables”?

Most of the times the answer is “User Variables”. That way even if the configuration gets messed up, you can use another account to restore the machine. Plus it helps keep separation of concerns. The other user doesn’t unexpectdly run a different file if their filename is same as yours.

On *—Nix

On *—Nix systems the naive and simplest way to edit PATH is by editing the rc file for your default shell. Most Linux distros have bash as their default and newers Macs have zsh as their default. Just add the following new line in your .bashrc or .zshrc accordingly:

export PATH="$PATH:/new/bin/path"

If you want a specific GUI application like GIMP to read these changes you can launch them from the CLI. If you don’t like it however then what are you are looking for is a way to import environment changes into the login session. The right steps to do so depend on your setup. I have mentioned an example in this blog post.

*—Nix users have to keep in mind the same thing as Windows users. Refrain from editing PATH anywhere outside your user directory so that even if things mess up you can use another account to recover.

Security Implication of modifying PATH

Since the directories are searched according to their listing order when running a command, if there are two files with the same name in the PATH, the one whose parent directory comes first wins.

This creates an issue if you blindly add new locations to the beginning of the PATH. If a developer knowingly or unknowingly ships a binary with the same name as a system utility and you run it with administrative privileges, you leave your entire machine vulnerable.

Hence, always add new directories below/after the existing (pre-configured) entries and stay safe.

Where there is a PC, there is a PATH

Happy Hacking ;)