Where do you keep your own scripts on OSX?

MacosBashTerminal

Macos Problem Overview


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?

Macos Solutions


Solution 1 - Macos

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

Solution 2 - Macos

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.

Solution 3 - Macos

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.

Solution 4 - Macos

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMore Than FiveView Question on Stackoverflow
Solution 1 - MacosPaul RView Answer on Stackoverflow
Solution 2 - MacosDavid W.View Answer on Stackoverflow
Solution 3 - MacosLriView Answer on Stackoverflow
Solution 4 - Macosuser1738671View Answer on Stackoverflow