Create and Use Bash Scripts: A complete guide

Make the most of this powerful tool

Photo by Obi Onyeador on Unsplash

Prerequisites:

  • Some knowledge of the Mac terminal (guide HERE)
  • This tutorial is going to use VIM (guide HERE), but you could use any text editor

Terminology

Bash: “Bourne again shell”. There are many shells that can run UNIX commands, and the one used on the Mac Terminal is Bash

A little help…

This tutorial is not an exhaustive guide to the bash terminal, and is rather a starter guide to scripting with some basic bash syntax. This guide does use VIM but you are certainly welcome to use other text editors.

Creating your bash script

Creating the bash script

We are going to create our first bash script (called, wonderfully myfirstscript). There’s an animated gif to help with this if you choose to scroll to the end of this section.

Add the shebang line

From within VIM we need to tell the command line to run the code using bash. VIM requires that you edit the current line, i to insert will do just that, then type away!

#!/usr/bin/env bash

Add the echo line

From VIM we add an echo line, which will (in the end) write “Hello,World” to the console.

echo "Hello,World!"

Save the script

From within VIM we can (if we were editing a quick nudge of the escape key will allow us to do this) use the save command :wq

Make the script executable

From terminal run the following to make the script executable

Running the script

After all of that, this isn’t too difficult

The video so you can see what to do? That’s right here:

Make the script Globally available

Running the script from this particular folder is great, but we really want to create utilities that can be run from anywhere on your device.

:wq

The basics

Strings

As we have seen a little earlier in this guide, we can use echo to print to the terminal. There aren’t any painful quotes here — we can just write (including any spaces) straight to the screen!

echo hello big scary world
echo [option(s)] [string(s)]

Shell execution

You can use the output of a shell execution in a script. This basically uses the output of a shell as a variable (more on that in a minute):

Arrays

Arrays can be used to store data link in any language. This doesn’t even need to be done in a script, as it can be run directly on the command like:

Variables

Variables are declared with just the variable name, but are called with $.

User input

If you want something a little more interactive, you might want to use user input. This is done using the read keyword. The following is an alternative script to the one above that writes out a wonderful greeting message that is personalised for you.

Comparison and if statements

Again using examples of greetings, we can demonstrate the implementation of an if statement. Now this is tricky, as bash treats strings and number differently; however for this simple example equality == will do.

Loops

Here we can list the files in the current directory.

Functions

To demonstrate the use of functions with parameters I’ll write a simple function to (in practice) replace the echo function

Debugging

Ok, this is only debugging of a sort.

#!/usr/bin/env bash
#!/usr/bin/env bash -x

Conclusion:

This is the start, not the end of bash scripting. This should give you some of the (albeit basic) tools to be able to create works of beauty on your machine — all from the command line.

The Twitter contact:

Any questions? You can get in touch with me HERE

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store