As I write my bash scripts for my OS X that do general things, I am wondering where is a good place to keep them. Is there a directory I can put them all in where they will be picked up automatically? Or should I create my own directory and then reference this directory from .profile or something?
4 Answers
Usually /usr/local/bin
, unless you don't want other users to have access to them, in which case $HOME/bin
.
/usr/local/bin
may be in the default PATH, but $HOME/bin
will certainly need to be added to PATH.
Adding
$HOME/bin
to PATH:
PATH=${PATH}:$HOME/bin
export PATH

- 210
- 6
- 16

- 208,748
- 37
- 389
- 560
-
How can I have the Mac look for programs in a folder other than /usr? I want to keep all packages (like brew, swiftlint, gitconfig) in a custom folder – May 21 '19 at 04:09
-
@DanielSpringer: that’s what /usr/local is for. You can put your stuff anywhere though, if you really want to - just add the relevant directory(ies) to your PATH. – Paul R May 21 '19 at 05:52
-
I downloaded swiftlint to `usr/local` and it worked fine, but as soon as I moved it to a different folder of mine (and added the path to that folder to `$PATH`) Xcode didn't find it (in other words, it was no longer found by the programs that use it). Perhaps I did something wrong when adding the path – May 21 '19 at 18:36
-
My current path is `/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:~/bin:/Users/dani/Documents/repos/dotfiles/bin` so if something is in `/Users/dani/Documents/repos/dotfiles/bin`, will it be found automatically? Just to avoid XY, my goal is to have all scripts from `usr/local/bin` and `~/bin`, in a different folder. (sbin stuff too). Does that make sense? – May 21 '19 at 18:38
-
1Yes, the motivation is still a mystery but this should work. – Paul R May 21 '19 at 19:47
I have my PATH set as:
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
$HOME/bin
I use /usr/local/bin
for commands that I have that override the default commands. For example, I have Subversion 1.7.7 installed, and the OS X comes with 1.6.18. The version 1.7.7 of svn
is in /usr/local/bin
while the default 1.6.18 version of svn
is in /usr/bin
. When I type svn
, I get the version I installed instead of the version that comes with OS X. I've done this with Java, Git, Python, and several other binaries where I need a different version that what came on my Mac. Most of these are symbolic links. For example:
$ ls -l /usr/local/bin/ant
lrwxr-xr-x 1 root wheel 16 Jun 12 11:01 ant -> /opt/ant/bin/ant
Ant 1.9.1 is installed in /opt/ant
(actually, /opt/apache-ant-1.9.1
, but it's symbolically linked to /opt/ant
). I linked all the stuff under /opt/ant/bin
to /usr/local/bin
, so it's in my path.
I use $HOME/bin
for my personal shell scripts and other scripts. Traditionally, you make this the last entry in your PATH, so you don't accidentally override a built in command. If I made a shell script command called cp
, I wouldn't override the /bin/cp
command.

- 105,218
- 39
- 216
- 337
-
5I put `$HOME/bin` at the front of my path so I can override system choices (and I complicate my path by having multiple versions of Perl and GCC available in separate directories, and defaulting to the most recent version — those appear after $HOME/bin), but otherwise, I go with a system similar to this. – Jonathan Leffler Sep 08 '13 at 16:09
-
@JonathanLeffler You should look at [Perlbrew](http://perlbrew.pl). It allows you to easily switch between various versions of Perl. For others, I put the binaries under in `/opt`, then link. For example, I have `/opt/apache-ant-1.9.1`, `/opt/apache-ant-1.8.1`, `/opt/apache-ant-1.7.1` The one I want to use, I link to `/opt/ant`. Then, I'll link binaries under `/opt/ant/bin` to `/usr/local/bin`. To switch versions, I merely change the link under `/opt` – David W. Sep 08 '13 at 19:25
-
@JonathanLeffler There's a tradition to keep `$HOME/bin` at the end of your path for _security reasons_. This was back in the days when everyone was on the same Unix box. It probably doesn't apply much anymore. I use `$HOME/bin` mainly for shell scripts, and other scripts. Binaries are installed elsewhere and linked to `/usr/local/bin` – David W. Sep 08 '13 at 19:27
-
2I put it at the front of my path for _security_ reasons; if I can't trust my bin directory, I've got problems that using the system directories won't fix. And I can override the crap^H^H^H^Hstuff that others place in `/usr/local/bin`, for example. In a previous job, I had to ignore `/usr/local/bin` (not on a Mac, per se) because it was 'maintained' by MIS, was NFS-mounted read-only, and usually contained way out of date (as in 'obsolete') programs — can you say Perl 5.004? – Jonathan Leffler Sep 08 '13 at 19:31
-
1Note that `$HOME` (or for that matter `~`) will not work in `/etc/paths`. It needs to be spelled out there — e.g., `/Users/Rax`. – orome Oct 04 '15 at 13:48
I use ~/bin
for executables and scripts I wrote myself and /usr/local/bin
for executables and scripts I didn't write myself.
/usr/local/bin
is used by Homebrew, pip
(and gem
and npm
installed by Homebrew), as the default target for make install
, and by some .pkg
installers.
I used to have a separate directory for executables and scripts I didn't write myself and that weren't placed in /usr/local/bin
by default. But it contained so few files that I moved all files in it to /usr/local/bin
.
I can find the non-Homebrew stuff in /usr/local/bin
with find /usr/local/bin ! -lname '../Cellar/*'
.
I don't use /usr/local/bin
for scripts I wrote myself, because /usr/local/bin
already contains about 1000 other files, and ~/bin
(or bin
) is often easier to type.
I have added setenv PATH ~/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/libexec:/usr/texbin
to /etc/launchd.conf
. Having /usr/local/bin
before the other directories is potentially dangerous, and for example some TextMate commands stopped working because of it, but it's also convenient to have newer versions of commands before the system-installed versions and to use the same path everywhere.

- 26,768
- 8
- 84
- 82
In my mind, you can put your scripts where you want. It dosen't really matters unless you used system specefic folders. I do keep mine in /scr with root only permission on that folder. It's conviniant because all the script in this folder needs root access to perform correctly and I don't want end users to peek into that folder.

- 77
- 1
- 8