If you are familiar with bash, but not familiar with the Mac OS X environment, then keep reading, this blog post is for you.
If you are using your Mac to do some serious software development, or if you would like to use your Mac to perform several IT administration tasks, you might want to automatise some of the tasks using scripts such as bash scripts.
Bash scripting is a popular option because of portability. Bash is available on all Unix and Linux Platforms and it was recently added to the Windows.
This short text is not about scripting, it is about the script file, and the advise about where it could be stored, and any relevant changes to the environment that would be required for running the script.
Let’s say you have the following script stored in a myscript.sh file:
#!/bin/bash
echo "Hello World!"
The file containing the script usually have the following:
myuser:~ myhost$ ls -al myscript.sh
-rw-r--r-- 1 myuser staff 0 Jan 02 16:24 myscript.sh
which indicates the script has read and write permissions “rw” for the file owner, read “r” permission for users in the “staff” group user read permission for all “other” users.
As you already know you need first to make the file executable. Let’s give it executable permission for the owner of the script
myuser:~ myhost$ chmod u+x myscript.sh
myuser:~ myhost$ ls -al myscript.sh
-rwxr--r-- 1 myuser staff 0 Jan 02 16:24 myscript.sh*
Now running ./myscript.sh will run the script:
myuser:~ myhost$ ./myscript.sh
Hello World!
But the question is now, where do we put the script and what do we have to change to be able to run the scripts from anywhere.
Option 1: /usr/local/bin/
The first option is to move your scripts to the /usr/local/bin/ folder that might already contain other scripts and executables. Using this option will allow other users on the device to access the script as well depending on their permissions. Since the path /usr/local/bin is usually part of the $PATH variable that is loaded in the terminal environment, you will be able to run the script from everywhere as long as the path is present in the current environment $PATH variable
myuser:~ myhost$ mv myscript.sh /usr/local/bin/
To check whether the $PATH already contains the path use the following command and check the output for the path
myuser:~ myhost$ echo $PATH
Option 2: $HOME/bin
In this option, we move the scripts to a special folder in the user’s own home folder $HOME/bin:
In this case, only you will have access to the script, and all your scripts will be isolated in one location, which is a good option for backup and deployment of the scripts.
The folder $HOME/bin/ does not usually exist, and thus it is not usually already in $PATH variable. Therefore the folder must be created and the path must be added to the $PATH variable of your environment.
myuser:~ myhost$ mkdir $HOME/bin/
myuser:~ myhost$ myscript.sh $HOME/bin/
To check whether the $PATH already contains the path use the following command and check the output for the path
myuser:~ myhost$ echo $PATH
If the path is not already included in $PATH open the $HOME/.bash_profile for editing. At the end of the file add the following line:
export PATH="$HOME/bin:$PATH"
Store the file. Close the terminal and re-open it again to reload the environment variables that automatically runs the $HOME/.bash_profile. Alternatively, you could reload the $HOME/.bash_profile by running:
myuser:~ myhost$ source $HOME/.bash_profile
That’s it basically.