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_profile
instead.
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
.
cd /Users/dmachado/Documents/MyDir
chmod +rx-w MyProgram
~/.zshrc
fileFrom 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.
~/.zshrc
or restart your terminalsource ~/.zshrc
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