Skip main navigation

New offer! Get 30% off your first 2 months of Unlimited Monthly. Start your subscription for just £35.99 £24.99. New subscribers only T&Cs apply

Find out more

Writing Your First Bash Script

writing script Hello World!
7.5
Hello, I’m Victoria Offord. And in this video, I’ll be walking you through how to write and execute your very first Bash script. Now, all a Bash script is, is just a text file that contains one or more Bash commands. In order to write your Bash scripts, all you need is a Unix terminal and a text editor. There are many different text editors available and some of the widely used editors include VS Code, Atom, Sublime, and there are terminal based editors such as vim or nano. In this video we will be using nano, which you were introduced to back in week one. All of your Bash scripts will be stored in files.
49.5
The name of these Bash script files can be broken down into two parts, the script name and the file extension. It’s worth noting here that the most widely used extension for Bash scripting is dot sh. Let’s get started with a simple Bash script that outputs hello world back to our terminal. The command that we need for this is nano, followed by a space, and then our file name. In this case, hello dot sh.
79.4
This will open up our text editor into which we can copy and paste the commands that have been provided. Once we’ve done this, we then need to save our file. In order to do this, you can press control and x, enter yes or y, and then hit enter to confirm that we want to save our file. It’s always a good idea to check the contents of your file, so we’ll just print the contents of the file to our terminal using cat followed by our file name. As you can see, the commands we copied and pasted are still there within our file. Now we have our first Bash scripts stored. There are several ways we can execute this Bash script.
123.3
First, we can type dot, forward slash, followed by the name of our file. In this case, dot slash hello dot sh. Now you’ll see here we’re using dot forward slash, which means we’re using the relative path. And this tells the system the file is in our current working directory. Alternatively, we could have used an absolute path. This means the full path to where our script is stored. For example, if our script was stored in the home directory, we could use the command tilde forward slash hello dot sh. Or alternatively, the full path to wherever else in our file system the script may be stored. Let’s see how this works in practise.
167.4
So now we’re going to start trying to execute our Bash script. First, we need to tell the system where to find our script. We use the relative path dot forward slash to look in our current working directory. Then we’ll enter the filename and hit enter to execute the script. Now you’ll see here that we’ve encountered an error saying that our permissions have been denied. Let’s take a closer look and see what’s going on. So when we tried to run a script file we got an error saying that permissions were denied. Now this is because our script file needs to be executable in order to run it. But when we create it, we don’t have the correct file permissions.
206.3
To update our file permissions, we can use the Bash command chmod. And we can tell it to make the file executable by adding the options plus x. Let’s see how this works in practise. Let’s start by updating our file permissions. We can do this using the Bash command chmod. First, types chmod followed by a space, and then use the options plus x to tell it that we want to make our file executable. Follow this with another space, and then the name of our script file, in this case, hello dot sh. Once we’ve done this, our script should now be executable and we can start to try running it again.
247.2
We use the same format as before, so dot forward slash, to say that we’re looking at our current working directory, and then following this with our script name, hello dot sh. Perfect, you’ve now said hello to the world. There are multiple ways that we can run our Bash scripts. In the previous example, we saw how to change our file permissions to make our script executable, and run it without having to directly call the Bash interpreter. If you’ve forgotten to make the file executable, you can always invoke the Bash interpreter directly by calling Bash, and then providing the path to your script file. Let’s see how this works.
287.8
In this example, we’re invoking the Bash interpreter directly as part of our execution command. Simply type Bash followed by a space, and then your script name. Hit Enter, and again you’ll see we’re saying hello to the world. OK, so now we know how to write and execute our Bash scripts, but what’s actually going on under the hood? To understand this, we need to take a look at the Bash commands our scripts contain. The example here, hello dot sh, contained two lines. The first is called the shebang, and this is present in any Bash script that you’ll write. Using the shebang, we tell the operating system which shall interpreter we want to use, in this case, the Bash interpreter.
336.2
In our second line, we are simply asking to print Hello world back to the terminal using the Bash command echo. There are several ways to invoke the Bash interpreter. The first is to use the absolute or full path to the interpreter in the shebang. Here we have two examples. The first looks for the interpreter in the bin directory, and the second in the user bin directory. However, if we define the absolute path to the interpreter this means we can have issues if we need to use the script on a system where the interpreter is located in a different place.
372.5
To make our scripts portable we use the following shebang format, which will use the first batch executable that it finds in our environmental path. The shebang is an important part of Bash Scripting. Always remember to include it at the top of your script files.

In this video, Victoria is presenting how to write a simple script.

You are encouraged to follow along and write your first script saying Hello World!

Here is the script presented in this video:

#!/usr/bin/env bash

echo "Hello world!"

Do leave any comments and questions in the comments section below.

This article is from the free online

Bioinformatics for Biologists: An Introduction to Linux, Bash Scripting, and R

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now