In part 1 of this series, I encouraged you to not fear Bash, in part 2, we dove into what Bash does. In part 3 we looked into the basics and a very brief glimpse at some of the basic tooling you can leverage. In this final part of the series, we are going to look at the most powerful aspects of Bash, scripting.
When I say this, do that
Bash can make your machine do all sorts of amazing things, but to be honest, it can feel overwhelming to need to type out every command and path of the tool you want to invoke every time. Fortunately, Bash comes through with a way to make your computer do the hard work for you. You can tell Bash to remember your own personal shortcuts in a file called .bash_profile on Mac and .bashrc on Linux. These shortcuts are called Aliases.
Aliases are very simple to set up and can save you a lot of typing. For example, an alias I use a lot is ‘gpom’ which I use to mean 'git push origin master'. When I push with Git, this alias saves me 18 characters while also reducing my possibilities of typing the wrong thing, which as a human is something I do more often than I care to admit.
Where it can start to become powerful is when leveraging Bash’s ability to chain multiple commands together, using the output of one command for the input of another with a pipe ‘|’ or, to redirect output, with a ‘>’. For example, an alias I use when I want to use a text emoji shrug is "alias shrug='echo " ¯\_(ツ)_/¯ " | pbcopy ' ". This prints the shrug, not onto the screen, but instead as the input to pbcopy which copies onto my clipboard to paste in anywhere I like.
But to make it very easy to input only a few characters and run a defined list of commands, and maybe use some logic along the way, we need to write a script.
Scripts, just like they used in Star Wars
When you watch one of your favorite films or TV shows, what the actors say and do is the result of reading and executing a script. Computers are really good at doing exactly the same thing, reading and executing things in a specific order. And they are really great at doing that same thing over and over and over again.
These can be very long and complex but these scripts can also be very simple. The first script I ever wrote looked something like this:
When executed it does this:
While a simple example, there is a lot to parse through here for someone new to scripting. Let’s start from the very top, with that funky looking line ‘#!/bin/bash.’ The first symbols we see are the “#!”, which together are pronounced “shebang”. This tells Bash that it should try and use this file as a script. The ‘/bin/bash’ part tells Bash which program should be used to interpret the file, which in this case is Bash itself, located in the default /bin folder on most every machine. Don’t worry if this is a little fuzzy to understand, it is very common to copy and paste this first line when writing a new script.
The script itself, helloworld.sh, is executed when I run the ‘./’ in front of the script name. The file itself we give the suffix ‘.sh’ to indicate it is a shell script. While the ‘.sh’ is not mandatory in all cases, it can make your life easier when working with a lot of different kinds of files across projects. The ‘./’ is a shortcut to tell Bash that we want it to interpret a file in the current ‘.’ or ‘dot’ directory.
There is one more step needed before we can actually run this or any script, and that is to tell Bash that it has permissions to interact with the file. This is done with ‘chmod’, which is short for ‘change file modes’. Without digging in too deep on this blog, a safe way to do this is to use the options ‘755’ or ‘+x’ to make the file executable.
That next line in the script, “echo”, just prints whatever follows it to the terminal screen. Echo is just a handy example, but any command can be added after that first line. There is no maximum or minimum size to a script. For example, a simple one I use a lot just executes two Terminus commands; one line to deploy committed changes from my Dev environment to my Test environment and one line to deploy from Test to Live.
This is where Bash becomes sufficiently advanced technology to look like some kind of magic to the uninitiated. Instead of just moving a file from point a to point b, we can call one tool on one machine to pipe data to another machine to kick off a whole other series of scripts that interact with other tools on other machines. The sky is the limit on how powerful these scripts can become, but they all work on the same principles that let us print “Hello Word!”
The more you automate, the more you can optimize
With scripting, you can start to automate the processes around Continuous Integration and Continuous Delivery. The Pantheon Build Tools scripting and the Example Terminus Auto Update Script are both excellent illustrations of Bash scripting we have made available showing off pretty complex processes that would take a long time to run manually.
Once we are freed from manual processes, we can observe and start to make changes to make the whole system more efficient. We can also find points to interject new tests or other tools that improve the overall quality of output. One of the really amazing parts of scripting is it is human readable, or at worst, we can make it be human readable with comments.
If I was going to do this with my example helloworld script, it might look like this:
The ‘#’ symbol means that everything after it is a comment after the first line. Bash will simply ignore it when it runs the script. The clearer you write these comments will help you and anyone else running your code. It is the difference between maintainable code that is easy to optimize and disaster scenarios of ‘spaghetti code’ that is impossible to maintain and extremely hard to understand.
The whole point of Open Source
As one colleague put it, "Bash is the point of Open Source. Leveraging smart tools other smart people have built." We are all standing on the shoulders of giants and there is an entire world of people who were where you are now, no matter where you are on your path.
There are so many resources to tap into. Here are my personal top 3 favorites:
Bash Academy - this one even has an interactive game to teach you Bash.
Most Bash users I have ever talked to have given me a friendly tip or trick to help me advance. It really is a universal language for almost all developers and a great way to break the ice when you are not sure how to start a conversation at an event. Just say out loud “I would like to learn more about Bash,” and the stories and tips will likely flow, no matter what the technical conference.
It has been a pleasure to write this series. No matter who you are, if you use a computer, learning Bash will come in handy. Hopefully, I have shown that there is nothing to fear and the benefits of learning this tool far and away outweigh any costs. Pantheon wants you to be able to unleash the power of your code and do amazing things.
Let us know in the comments if you have other suggestions to help people master Bash.
You may also like: