1

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?

0

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
  • could i add it to .bash_profile instead of .bashrc? – Cody Rutscher Aug 23 '18 at 15:40
  • @CodyRutscher As you mentioned, you are looking for the best approach. I'd recommend adding it to ~/.bashrc instead of ~/.bash_profile. ~/.bash_profile is executed for login shells, while ~/.bashrc is executed for interactive non-login shells. That means ~/.bashrc is executed every time you open a new tab/window in Terminal.app. – Nimesh Neema Aug 23 '18 at 15:59
  • 1
    @NimeshNeema ~/.bash_profile is used every time you open a new tab in Terminal. Each new instance is via login and so ~/.bashrc is not sourced apple.stackexchange.com/questions/119711/… – user151019 Aug 23 '18 at 22:05
  • @Mark Thanks for pointing out. I totally missed that. I'll update the answer. – Nimesh Neema Aug 23 '18 at 22:07
  • @NimeshNeema This is almost certainly a suplicate better to find this and close the question as a dup. – user151019 Aug 23 '18 at 22:09
1

If you want to one line it (which I find more convenient):

echo 'export PATH=$PATH:"/path/to/your/dir"' >> ~/.bash_profile && source ~/.bash_profile

of course you can change this to your preferences (ie. append or prepend to $PATH, .bashrc, .bash_profile, .zshrc, etc.)

  • 2
    You just overwrote anyother setting that had previously been declared in .bash_profile – fd0 Aug 23 '18 at 19:52
  • 1
    @fd0 oh you're right. I'll fix that – Cyb3rL0rd Aug 23 '18 at 21:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .