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?

Nimesh Neema
  • 51,809
Cody Rutscher
  • 193
  • 3
  • 14

2 Answers2

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.)

Cyb3rL0rd
  • 11
  • 3
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
Nimesh Neema
  • 51,809
  • 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 https://apple.stackexchange.com/questions/119711/why-doesnt-mac-os-x-source-bashrc/119714#119714 – mmmmmm 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. – mmmmmm Aug 23 '18 at 22:09
  • If .bashrc is "executed", I think you mean sourced, in every Terminal.app window or tab then why would you need to source it in .bash_profile? – fd0 Aug 24 '18 at 13:53