Beginners Guide To BASH - Part 1 - Hello World


Welcome to the beginners guide to BASH.

There are lots of guides on the internet showing how to create Shell scripts using BASH and this guide aims to give a slightly different spin because it is written by somebody who has very little shell scripting experience.

Now you might think that this is a silly idea but I find that some guides talk to you as if you are already an expert and other guides take too long to cut to the chase.

Whilst my LINUX/UNIX shell scripting experience is limited, I am a software developer by trade and I am a dab hand at scripting languages such as PERL, PHP and VBScript.

The point of this guide is that you will learn as I learn and any information I pick up I will pass on to you.

Getting Started

There is obviously a lot of theory that I could pass on to you straight away such as describing the different types of shell and the advantages of using BASH over KSH and CSH.

Most people when learning something new want to jump in and start with some practical lessons first  and with that in mind I'm not going to bore you with trivia that isn't important right now.

All you need for following this guide is a text editor and a terminal running BASH (the default shell on most Linux distributions).

Text Editors

Other guides that I have read have suggested that you need a text editor which includes colour coding of commands and the recommended editors are either VIM or EMACS.

Colour coding is nice as it highlights commands as you type them but for the absolute beginner you could spend the first few weeks learning VIM and EMACS without writing a single line of code.

Out of the two I prefer EMACS but to be honest I prefer to use a simple editor such as nano, gedit or leafpad.

If you are writing scripts on your own computer and you know you will always have access to a graphical environment then you can choose the editor that works best for you and it can be either graphical such as GEdit or an editor that runs direct in the terminal such as nano or vim.

For the purposes of this guide I will be using nano as it is installed natively on the majority of Linux distributions and it is therefore likely that you will have access to it.

Opening A Terminal Window

If you are using a Linux distribution with graphical desktop such as Linux Mint or Ubuntu you can open a terminal window by pressing CTRL + ALT + T.

Where To Put Your Scripts

For the purposes of this tutorial you can put your scripts in a folder under your home folder.

Within a terminal window make sure you are in your home folder by typing the following command:

cd ~

The cd command stands for change directory and the tilde (~) is a shortcut for your home folder.

You can check that you are in the correct place by typing the following command:


The pwd command will tell you your present working directory (where you are in the directory tree). In my case it returned /home/gary.

Now obviously you won't want to put your scripts straight into the home folder so create a folder called scripts by typing the following command.

mkdir scripts

Change into the new scripts folder by typing the following command:

cd scripts

Your First Script

It is customary when learning how to program to make the first program simply output the words "Hello World".

From within your scripts folder enter the following command:


Now enter the following code into the nano window.

echo "hello world"

Press CTRL + O to save the file and CTRL + X to exit nano.

The script itself is made up as follows:

The #!/bin/bash needs to be included at the top of all the scripts you write as it lets interpreters and the operating system know how to handle the file. Basically just remember to put it in and forget about why you do it.

The second line has a single command called echo which outputs the text that immediately follows it. 

Note that if you want to display more than one word you need to use double quotes (") around the words.

You can now run the script by typing the following command:


The words "hello world" should appear.

Another way to run scripts is as follows:


The chances are that if you run that command in your terminal straight away you will get a permissions error.

To grant permissions to run the script in this way type the following:

sudo chmod +x

So what actually happened there? Why were you able to run sh without changing permissions but running ./ caused an issue?

The first method loads the bash interpreter which takes the as an input and works out what to do with it. The bash interpreter already has permissions to run and just needs to run the commands in the script.

The second method lets the operating system work out what to do with the script and therefore it requires an executable bit in order to execute.

The above script was ok but what happens if you want to display the quotation marks?

There are various ways to achieve this. For instance you can put a backslash before the quotation marks as follows:

echo \"hello world\"

This will produce the output "hello world".

Wait a minute though, what if you want to display \"hello world\"?

Well you can escape the escape characters as well

echo \\"\"hello world\\"\"

This will produce the output \"hello world\".

Now I know what you are thinking. But I really want to display \\"\"hello world\\"\"

Using echo with all these escape characters can get quite silly. There is an alternative command you can use called printf.

For example:

printf '%s\n' '\\"\"hello world\\"\"'

Note that the text we want to display is in between single quotes. The printf command outputs text from your script. The %s means that it will display a string, the \n outputs a new line.


We haven't really covered much ground in part one but hopefully you have your first script working.

In the next part we will be looking at improving on the hello world script to display text in different colours, accept and handle input parameters, variables and commenting your code.

More From Us