Create and Use Bash Scripts: A complete guide
By: Steven Curtis
Tags: Software Engineering, Software Development, Development, Programming

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

The syntax for echo is as follows:

echo [option(s)] [string(s)]

But how can we see what the options are? We have a choice — the manual through man or the short documentation through help echo . Which would I recommend? At this point the shorter guide is probably enough

$ help echo

Using these parameters does bring back the quotations, simply because we need to use the escape characters

$ echo -e "\a Hello, World"

in the documentation \a gives an alert. For me, this gives a nice sound from the Mac and a bell symbol at the top of the Terminal screen (if I use iTerm).

Speaking of which, it looks like the following:

Multiple options? See the following:

$ echo -e “\a

Hello, World”

Which puts our message on a new line. Escaping quotation marks will work…

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):

$ echo "Hello, $(whoami)"

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:

$ languages=(‘Swift’ ‘Python’ ‘Java’ ‘JavaScript’) echo ${languages[3]}

This will predictably print Javascript (due to zero-indexed arrays)

Variables

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

I’ve set up a new script called myvariablesscript, and made it executable with chmod +x myvariablesscript .

within the script I’ve added the following code

Which means we need to provide a name, which it will say hello to whichever variable is passed.

Running with ./myvariablesscript :

Input: “Me”

expected output: Hello to Me

This is fine. But if you don’t provide a name? It will just output Hello to. This isn’t great.

So we can provide a default message if no name given:

Now if you don’t provide a name the following error is given:

./myvariablesscript: line 3: 1: Error: no name given

Can we do even better? Yes, yes we can.

We can provide a default variable:

so if no parameter is provided we see Hello to ME

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.

which, if your name is given as You will return Hello, you! and if not returns Good Morning followed by your name.

Here are the Comparisons for Number and Strings

The operator for numbers works the same way as that for strings, that is should be between two operands that is [20 -eq 20] resolves to true whereas [21 -eq 20] resolves to false.

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

If we wanted more parameters we would refer to them sequentially, that is $2, $3 and so on.

Debugging

Ok, this is only debugging of a sort.

At the top of each file instead of using

#!/usr/bin/env bash

we can use

#!/usr/bin/env bash -x

to get some rather juicy information!