PATH – Adding a New Directory to PATH Variable

binarypath

What is the de-facto way to export a binary/executable to my global PATH? I have seen different answers to this, but I am looking for the best answer.

Basically, if I go on Github and download a directory or folder and then I want to add whatever I download to my PATH, what is the recommended way to do that, so that I can echo it in any shell?

Best Answer

Add the directory in PATH variable in ~/.bashrc file. When an interactive, non-login shell is started, ~/.bashrc is executed. That means ~/.bashrc is executed for every new tab/window opened in Terminal.app.

Simply add this line in your ~/.bashrc file:

PATH=$PATH:~/DirectoryName
PATH=~/DirectoryName:$PATH

as per your preference. If the directory name is prepended, any system executable of similar name may get shadowed.

To execute ~/.bashrc and get the new PATH in effect without restarting terminal, execute:

source ~/.bashrc

Also, make sure to enter this line in your ~/.bash_profile

if [ -f ~/.bashrc ] ; then
    . ~/.bashrc
fi