Category: Ubuntu

Added: 18th of December 2015

Viewed: 2691 times

Create a simple bash script in Ubuntu

This tutorial works in Ubunbu, Kubuntu, Xubuntu, Lubuntu, Ubuntu Mate and other Ubuntu based distributions. The tutorial also works in Linux Mint and other Debian based distributions.

Bash scripts allow to automate tasks in Ubuntu. A good example of using a bash script could be to install a bunch of packages after reinstalling Ubuntu.

To create your first bash script in Ubuntu follow the instructions below.

We first need to identify the path to bash, so open your terminal and enter the following command

which bash

It will return with /bin/bash, this is what you will enter at the top of every new bash script you create.

Open a new text document and copy and paste the code below.
#!/bin/bash
echo "My First Bash Script"

Save the text document as my_first_bash_script.sh to your desktop so you can easily locate it.

Before we can run the bash script we need to make it executable.Open you terminal and change directory to your desktop using the following command
cd /home/{username}/Desktop

Remeber to replace {username} with your Ubuntu username

Make the file executable my issuing the following command. What this command does is give you read and write permissions on the file.
chmod +x my_first_bash_script.sh

The next part is to run the bash script. In the terminal enter the following command
./my_first_bash_script.sh

Other Tips & Tutorials

Ubuntu

How do I upgrade a single package in Ubuntu and Ubuntu based distributions

Posted: 12th of September 2021

Views: 248

Ubuntu

Create a multiboot USB for your .iso images, using Ventoy on Linux

Posted: 25th of August 2021

Views: 340

Ubuntu

How to find out what date you installed Linux on your machine

Posted: 13th of April 2021

Views: 476

Ubuntu

Install bashtop in Ubuntu and Ubuntu based distributions. Monitor your cpu, memory, disks, network and other processes

Posted: 21st of February 2021

Views: 587

Ubuntu

Ubuntu 20.10 Groovy Gorilla installation error, installer crashed

Posted: 3rd of February 2021

Views: 972

Ubuntu

Find out what version of Ubuntu you are running, using the terminal

Posted: 29th of January 2021

Views: 483

Ubuntu

Install Redshift to automatically adjust the colour of your screen depending on time of day on Ubuntu and Ubuntu based distributions

Posted: 26th of November 2020

Views: 1078

Ubuntu

PulseAudio, Pavucontrol not saving settings after reboot on Ubuntu and Ubuntu based distributions

Posted: 11th of November 2020

Views: 3752

Ubuntu

No sound after installation of Ubuntu or Ubuntu based distribution, install pavucontrol

Posted: 9th of November 2020

Views: 585

Ubuntu

Shutdown Ubuntu and Ubuntu based distributions at a specific time or within (x) minutes

Posted: 30th of October 2020

Views: 670