23

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.

1

3 Answers 3

24

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 ~/
8
  • If I add export PATH=/Users/thi/bin:$PATH to my .bashrc file, is it gonnae work? Is it correct?
    – Thi G.
    Aug 23, 2013 at 12:50
  • 12
    Better use $HOME/bin instead of /Users/thi/bin. You'll then be able to copy your settings to another machine, where your user name is perhaps different.
    – lhf
    Aug 23, 2013 at 13:00
  • If I type in the terminal: PATH=$HOME/bin:$PATH and then type: export PATH. Will it do the trick?
    – Thi G.
    Aug 23, 2013 at 14:31
  • 1
    If you do that, it will change the settings now, but not on future boots. To change the path in a way that survives reboots, you need to include those commands in your .profile file.
    – Daniel
    Aug 23, 2013 at 16:30
  • 2
    Great, thank you all for the answers. I added export PATH=$HOME/bin:$PATH to my $HOME/.bash_profile.
    – Thi G.
    Aug 24, 2013 at 12:46
10

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.

4
  • Great, thks for the answer. I added export PATH=$HOME/bin:$PATH to my $HOME/.bash_profile.
    – Thi G.
    Aug 24, 2013 at 12:43
  • You don't need export, and both ~ (when unquoted) and $HOME are expanded before the variable assignment both when using export or a plain assignment.
    – Lri
    Aug 24, 2013 at 18:10
  • Lauri Ranta, what do you prefer personally, .bash_profile or .profile? Or you have a .bashrc in your home directory?
    – Thi G.
    Aug 25, 2013 at 17:49
  • I used to use only .bash_profile, but now I use .bashrc as my main configuration file and my .bash_profile just contains . ~/.bashrc. New shells are opened as non-login shells in the shell mode in emacs and in tmux. I wouldn't use .profile as my main configuration file, because it is also read by other shells like ksh.
    – Lri
    Aug 26, 2013 at 7:52
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.

1
  • 4
    One might easily argue that you would put $HOME/bin at the beginning exactly for that reason. But the key thing is that the order of directories in $PATH matters and that the shell will pick the first matching command. Thanks for pointing this out.
    – nohillside
    Aug 23, 2013 at 17:42

You must log in to answer this question.

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