How to add a directory to your path on Linux or macOS.
Sometimes you need to permanently add something to your path. This is done with an EXPORT
command placed in your login script file. Which login script file you use depends on a number of things but if you are on Linux it will likely be ~/.profile and if you are on macOS it will likely be ~/.zprofile.
In the examples that follow, I will open .profile in the Nano editor and there add the directory /opt/foobar to my path. Feel free to use the login script file and the editor of your choice.
Open your login script file in an editor:
nano ~/.profile
If .profile exists, it will open in the editor. If the file doesn’t exist, the editor will help you create it.
Add this line:
export PATH=/opt/foobar:$PATH
…changing the directory as desired. Save the file and close the editor.
Force the system to read the new version of the file with:
. ~/.profile
Notice the command begins with a dot and a space.
Check your work:
echo $PATH
Your current path will be printed to stdout and should include the directory you added.
If your login script file isn’t already in your backup set, add it.
Additional information
The instructions above edit the path for your user. If you need to edit the paths for all users on the computer, see How to Add a Directory to Your $PATH in Linux, which despite the title also applies to macOS. The same article provides more information on what is $PATH, and how else to check and manipulate it.
Login script files are per-shell. Most people use only the default shell of their OS, so the information above should be enough for most people using mainstream versions of Linux or recent versions of macOS. If you use a nondefault shell on your OS, or more than one shell, or an odd or old OS, finding which login script file(s) you need should be easy but is up to you.