Preamble

In Linux or in macOS, there are many reasons why you may want to manually add a directory to the system's PATH variable so that executables in there can be called from anywhere in your computer. Here we show how to do that by editing the ~/.zshrc file in macOS. For most Linux distributions, however, you should use the files ~/.bashrc or ~/.bash_profileinstead.

Step 1: Get the full path to the directory

Ideally, the directory's path should have no spaces or special characters. Let us say, for example, that my directory is /Users/dmachado/Documents/MyDir and that is contains a program called MyProgram.

Step2: Make the program executable

cd /Users/dmachado/Documents/MyDir
chmod +rx-w MyProgram

Step 3: Open the ~/.zshrc file

From your terminal, you can open this file with vi or nano, but nothing impedes you from editing it with another application. Add the following line to that file:

export PATH="${PATH}:/Users/dmachado/Documents/MyDir"

Save and close.

Step 4: Source ~/.zshrc or restart your terminal

source ~/.zshrc

Step 5: Test

Move to the home directory and try to call your program from there.

cd ~
which MyProgram

You should see something like this:

/Users/dmachado/Documents/MyDir/MyProgram