Skip to content

Bash Scripting#

We'll learn bash scripting first because there are only a few core programming concepts we need to understand at this point in time:

Key Points#

We'll look at these in turn and then write a script to bring it all together.

Before we start#

Because you're writing a bash script, which is going to be something you'll work with a lot in the Linux space and Cloud in general, this code won't run on Windows in its default form. There are ways of making it so that you can run bash on Windows but instead of doing that, upload the file to your Ubuntu VM or write it directly on the VM using nano, vim, emacs, or some other editor that you like the looks of. This way you're working in bash's native environment and there shouldn't be any surprises.

Note

When you installed your local VM, you had the option to install a desktop environment. If you opted to do so, then VSCode is something you'll be able to run inside the VM.

A simple way of approaching would be to write the bash script on your host system (the system "hosting" the Ubuntu VM) and then use scp to transfer it to your VM. It's left to you, dear reader, to decide how you want to approach this problem.

Our script#

Let's start with a simple script and then explain it.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash

yourname=$1
# yourname = $1 <- this is invalid due to spaces around the '='

hello() {
  echo "Hello"
}

world() {
  echo "world"

  # This is invalid as 'return' has to be a number and means something else:
  # return "World"
}

hello
world

echo "$(hello), $(world)! My name is ${yourname}"

I've saved this file as simple_script.sh, but you can call it anything you like. There's not as much here as you might think. Here's the output:

1
2
3
4
$ ./simple_script.sh Michael
Hello
world
Hello, world! My name is Michael

Let's cover what we can see in the code, as that will help us understand the output.

Comments#

Every time you see a # followed by some text, then you're looking at a comment. Comments are ignored by the interpreter or the compiler. That's because they're designed to allow developers to

The "shebang"#

The first line is #!/bin/bash and is known as a "shebang". Technically, the shebang is the #!, and everything after that is a path to an executable. That executable is called and passed the contents of the script. The use of #! is used to tell the operating environment (bash in this case) that the file should essentially be considered as an executable.

The executable - in our case /bin/bash - doesn't have to be bash. You can essentially use any executable, such as Python:

1
2
3
#!/usr/bin/env python3

print("Hello, world!")

And the file will be treated as an executable.

All of this means you can set the executable flag on the file (+x) and then run it as if it were a real executable/binary: ./simple_script.sh as opposed to bash simple_script.sh.

The same applies to the use of the Python shebang. You can type ./python_script.py instead of python python_script.py.

You can also omit the shebang entirely and just use bash simple_script.sh. This will achieve the same result.