A more sane way to modify your PATH

Published: November 16, 2020   |   Read time:


Image Attribution:

The PATH environment variable is key for getting software to run on your computer. Sometimes you have to edit it by hand for your development purposes. Here is a tool to make that process a little more sane.

What is my PATH

Environment variables are global variables that your computer makes use of to make your system run smoothly. A common example is your home directory so programs know where to read or write files to. On Unix/Linux systems, the $HOME environment variable is something like /home/<username>/. On macOS $HOME may be /Users/<username>/, and on Windows the USERPROFILE environment variable is most often C:\Users\<username>\.

Environment variables are also used by your system to run executables. If you open up a terminal window and type ls, it’ll list the files and folders of the directory you’re currently in. But how does the system know what ls is, where it’s located, and how to run it? The system knows what ls is because the ls executable file is located in a folder in the PATH environment variable. It contains all the folders the system should look for when you go to execute a command.

If you run which ls on most OSs, you’ll see a result like /usr/bin/ls. If you print the variable with you will see a bunch of folders separated by a : (; on Windows) with /usr/bin located somewhere in there.

$ which ls

$ echo $PATH
                                                                                                                                                 # ^ ^ ^ ^

If /usr/bin wasn’t in the PATH variable, you would have to type out the full path to ls every time you wanted to run it. So having folders saved in your PATH makes working with your computer much less verbose.

How does PATH work

PATH works by checking each folder sequentially for the executable you’re trying to run. The folders at the beginning of PATH get checked first, so they have higher priority. If it’s not found in the first folder it tries again in the second one, then the third, until it reaches the end. This means if you have two versions of some program that are saved to two different folders, your system will always run the one in the folder located earlier in PATH. If the executable isn’t found in any of the folders, you’ll get an error.

You should be able to see how environment variables are useful, especially PATH. Most of the time you don’t need to worry about them, but sometimes you need to edit these variables manually. For example, if you’re testing out new software but don’t want to install it to a protected folder like /usr/, you can install it to a folder in your PATH and still execute it easily. If you want to install stuff to a particular folder that isn’t already in your PATH, you can add a new folder by editing PATH manually.

$ echo $PATH

# add this new folder to the beginning of PATH
$ export PATH="/path/to/my/new/dir:$PATH"
$ echo $PATH
# ^ ^ ^ ^ ^ ^ ^ ^ ^

The problem with editing PATH

While editing environment variables like PATH by hand can be great, there are some dangers that come with it. The easiest is making a typo, which makes you think your new PATH should work properly but actually doesn’t. The next easiest is making it a single directory and not including all the previous values. Above, if I had typed export PATH="/path/to/my/new/dir" instead of export PATH="/path/to/my/new/dir:$PATH", I would have suddenly lost access to ls, cat, and hundreds of other basic tools the system needs to work properly.

There are also more subtle things that can happen, and things that are annoying to do by hand. Some examples are:

  • removing a directory
  • deciding whether it should be added to the beginning or end of the list
  • changing the priority of a directory (its position in the list)
  • removing duplicate listings of the same directory
  • undoing the change you just made

To address these problems and to make working with PATH easier, I’ve made pad-path.

Introducting pad-path

With pad-path, you can easily modify your PATH environment variable on Linux, macOS, and Windows systems.

  • pad add
    • Add a new directory
    • Check for typos and that the directory you want to add exists
    • Prepend or append to give the new directory highest or lowest priority, respectively
    • Use relative paths for fewer keystrokes
  • pad rm
    • Remove a directory
    • Check for typos and only remove the directory you specify, leaving everything else untouched
  • pad up and pad dn
    • Change priority of a directory already in PATH
    • Move directories up or down in priority by a specified number of positions, leaving everything else untouched
  • pad clean
    • Remove duplicates and non-existent directories
  • pad ls
    • Print your entire PATH more legibly
  • pad undo
    • Undo the most recent change in case you made a mistake
    • Makes use of the OLD_PATH environment variable to keep your most recent change with pad

If this sounds useful to you, check out the GitHub repository or the Rust crate on crates.io. As with all of my software, pull requests and feedback are welcome.