You may on occassion feel the need to automate certain processes or proceudres on your linux based computer. These maybe simple processes like ensuring your packages are updated daily or even creating a useful utility to install libraries onto many computers.

These are occassions when writing and executing BASH files are extremely useful, to help reduce effort required. In this post I will provide a short tutorial on how to write and execute a bash script on ubuntu.

What is a BASH script

A Bash script is a ASCII (plain) text file which contains a series of commands. BASH is an acronym for “Bourne-Again Shell”, and is widely distributed as the shell for the GNU Operating system and is the default shell on Linux and OS X. Shell, is a program that takes your commands from the keyboard and passes them to the operating system to perform.

BASH commands can consist of multiple commands we would normally type ourselves using the terminal window, that you would like to execute together. You don’t need a complicated tool to create BASH file, any text editor will suffice i.e. Gedit – the text editor shipped with ubuntu.

For a great reference to the Bourne-Again Shell check out The Bourne -Again Shell

How to write a BASH script

Instead of starting off with a regular “Hello World” sample, I’ll provide with you a useful script you can run everyday to keep you ubuntu computer. If you’ve ever used the apt-get commands to do this process, you’ve partly written the contents of this BASH script.

To develop your BASH script simply open Gedit, your text editor shipped with Ubuntu and enter the following code snippet.

This script should be fairly familiar to anyone who  has frequently used to terminal to update their computer to using the latest packages.   This is a very simple bash script, starting off at line 1 with what is called a Shebang, what this does is tell the shell to interpret this file as a script and use the BASH. The following lines simply update your package repositories, upgrade any packages that require updating and then update your distribution packages. The last line simply removes any outdated packages.

To save your bash script, simply File –> Save As … –>

There is a convention to give Bash Scripts a .sh file extensions. Although Linux is an extensionless system, this extension is primarily for the Users benefit to enable them to identify bash files in a directory.

How to execute bash scripts

This tutorial assumes you have saved your file to your Home Directory. If you haven’t saved the file to your Home directory then you will need to change to the directory you have saved your file to by using cd YourDirectoryName

We’ll just need to change permissions on the file to allow execution by users.

There are two methods to execute a bash script, we could execute the file by explicitly telling the system to use BASH to execute the file.  You can use this method if you have omitted #!/bin/bash at the begining of the file:

Our script still need Super User permission to execute because we are executing Super User functions.  We can execute our script without the bash extension.

Our script is now available to execute anytime we would like to update our system. We can now also further improve our script with additional steps we need as time goes on. Below is a sample of the script I execute regularly on my machines to keep them up to date incorporating removal of all unused Linux kernel headers, images and modules from ubuntu boot

This script essentially does the same as above, but with additional step of removing any outdated linux kernels, free-ing up disk space, once again I run the update procedure and finally we check whether we should reboot the machine.

Although this is a very simple example of a bash script, it does serve to illustrate how to use bash scripts to automate mundane and time consuming tasks. As a programmer, you’re trained to identify and automate, improve or eliminate inefficient processes or tasks and to implement efficiencies and improve productivity, bash scripts are a tool to help you quickly and easily implement changes quickly.

Gary Woodfine

Freelance Full Stack Developer at
Helps businesses by improving their technical proficiencies and eliminating waste from the software development pipelines.

A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.

Affiliate Disclaimer

Disclosure: Please note that some of the links included in the blog posts are affiliate links, which means I will earn a commission if you decide to make a purchase.

I only include affiliate links to products, services and companies that I have personal experience and have actually used. I only recommend them because they are helpful and useful, not because of the small commissions I make if you decide to buy something.

Please do not spend any money on these products unless you feel they are relevant, suitable and will help you achieve your goals.

Buying anyone of these products and the commisions earned will not make me an overnite multi millionaire, but they will help to cover the hosting costs and compensate for the countless hours and effort I put in to create meaningful and valuable free content I provide to help others succeed.

You've also undoubtedly noticed that I also use various advertising networks - unless of of course you're using an Ad blocker, this is also an attempt to reduce my monthly deficit I experience in providing free relevant, valuable and on occassion insightful content for the benefit of others.

I only really make some money if people click on the ads, unless of course you consider 1c per 1000 impressions real money!

Bear in mind just how many impressions I need to make before I can cover my £20 hosting costs a month!

If you are using an adblocker and guilt stricken you can of course donate using any of the buttons below, but I won't be holding my breath.

Buy me a coffeeBuy me a coffee