What is bash scripting? One of the biggest benefits to learning the command line interface is being able to automate common, repetitive tasks through shell scripting. Any process or steps to execute can be compiled into a script to be run at will, or even at a scheduled time. This blog post will give a short introduction into writing basic scripts for the Bash shell, as it’s the most common default shell for Linux and Unix-like systems. The principles of shell scripting can be applied to both a *nix-based remote server or on your local system.
In this blog post, we’ll write a very simple script that takes any and all screenshots located on the Desktop, and moves them into a specified directory.
Writing our shell script
The first step to writing our shell script is by creating a new file in a text editor and adding the following line:
This line is what is known as a “shebang”. This tells the interpreter where your copy of the Bash shell is located (i.e. /bin/bash), which it will use to execute the script. You can double-check to make sure this is where bash is located on your system, by typing the following command in the terminal:
The output after entering the command will be the absolute file path for bash. Use this in place of /bin/bash if it differs.
The next step in our script is to create a directory to move any screenshots into. We’ll create this directory on the Desktop, although the location can be altered as per your preferences. Place this on the next line of the script:
mkdir -p ~/Desktop/screenshots
The -p flag indicates that the directory will only be created if it doesn’t already exist, and if it does, the command will be ignored/bypassed.
We’ll then move into our screenshots directory on the next line:
Finally, we’ll move all of our screenshots into the directory. This tutorial is based on the naming conventions for screenshots on OS X, although this can be modified as needed:
mv -f ~/Desktop/Screen\ Shot* ~/Desktop/screenshots
This tells Bash to move any files on the Desktop that begin with “Screen Shot”.
Saving and executing our script
As a general convention, shell scripts are typically saved with a .sh extension, although this is somewhat optional.
After saving the script, we’ll need to modify user permissions on the file in order to make it executable. Assuming the script was saved with the filename ssc.sh, we would run the following command to change its permissions:
chmod u+x ssh.sh
After execution permissions have been updated, we can now run our script. Assuming we are in the same directory as the script, we can simply run:
(. stands for the current directory)
Adding scripts to our $PATH
While this is functional, it will get a little tedious having to navigate to or specify the full path for our script each time we want to execute it. We can remedy this by creating a directory specifically for our own scripts (such as ~/bin/), and then add this file path to our shell’s $PATH.
If you are using Bash as you default shell, you can do this by adding the following line to your ~/.bash_profile:
We can now execute our screenshot cleanup script wherever we are, regardless of the current directory:
Writing shell scripting is enormously useful for automating all sorts of tasks. It can be applied to automating backups, file organization and maintenance, logging and much more. The more you start to automate simple tasks, the more you will wonder how you got by before without it.