OS X - Create a personal bin directory (~/bin) and run scripts without specifying their full path

So, what I am trying to do is to create a personal bin directory at my home folder. I created the following folder:

/Users/thi/bin

I put my scripts in this folder, but if I type in the Terminal:

myScript

It doesn't run.

I was told I have to export this bin path, something related to echo $PATH, but I don't know how to do it.


Solution 1:

You need to add the following to file ~/.profile:

export PATH=/Users/thi/bin:$PATH

Then source ~/.profile

Note, that you may need to create this file, and because it begins with a . it might not be visible in the finder for editing via an application like a text editor. To list all files including hidden ones, use:

ls -la ~/

Solution 2:

If you use bash, add a line like this to ~/.bash_profile:

PATH=~/bin:$PATH

PATH is marked for export by default, so you don't need to use export.

If both ~/.bash_profile and ~/.profile exist, bash only reads ~/.bash_profile when it is invoked as an interactive login shell. ~/.profile is also used by other shells that might not understand the same configuration options as bash.

Terminal and iTerm 2 open new shells as login shells by default. When bash is invoked as an interactive login shell, it reads ~/.bash_profile but not ~/.bashrc. The terminal emulators on other platforms often open new shells as non-login shells, so for example bash reads ~/.bashrc but not ~/.bash_profile. OS X users often use ~/.bash_profile as the personal configuration file corresponding to ~/.bashrc on other platforms, but it is also common to source ~/.bashrc from ~/.bash_profile or to tell Terminal or iTerm 2 to open new shells as non-login shells.

You can also change the path more globally in /etc/paths or /etc/launchd.conf, but in many contexts the path will still default to /usr/bin:/bin:/usr/sbin:/sbin. And I have only really needed to change the path in shells and text editors.

Solution 3:

You typically would put additional paths after $PATH like PATH=$PATH:/$HOME/bin because if you don't want to supersede another executable with the same name that might be in /bin, etc.