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?
If any user on the system should be able to run the script, put it in /usr/local/bin . Don't put scripts you write yourself in /bin or /usr/bin . Those directories are intended for programs managed by the operating system.
To add computer-level scripts to the script menu, save them into the /Library/Scripts/ folder on your Mac. For quick access to this folder, select Open Scripts Folder > Open Computer Scripts Folder from the script menu. When you do this, the folder is automatically created if it doesn't already exist.
In the Script Editor app on your Mac, choose Script Editor > Preferences, then click General. Select the “Show Script menu in menu bar” checkbox, then make sure the “Show Computer scripts” checkbox is selected. The Script menu appears on the right side of the menu bar.
The "official" place for local executables is /usr/local/bin . This directory is usually in the $PATH of all users by default. Traditionally, programs that are not installed through a package manager (eg apt ) are stored in the /usr/local/bin directory and those installed by the package manager in /usr/bin .
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.
$HOME/bin
to PATH: PATH=${PATH}:$HOME/bin export PATH
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With