Bash Scripting for Beginners: Learn how to automate repetitive tasks efficiently

Bash Scripting for Beginners: Learn how to automate repetitive tasks efficiently

In this article, you will learn how to automate repetitive tasks using bash scripting

ยท

4 min read

Whether you're a beginner or just starting with automation, this guide will teach you how to handle workflow routines like a pro.

Pre-requisites

This tutorial is suitable for beginners and people who have prior coding experience.

To follow along with this lesson, you should have the following installed on your computer:

Git Bash

Gitbash is a command-line tool that provides a bash shell environment, allowing you to write unix-like commands on your Windows computer.

If you do not have it installed, click here and follow the prompts to install it.

Code Editor

VS Code is a lightweight and powerful code editor developed by Microsoft. It is free and open-source.

You can use any code editor, provided you select it as your default code editor when installing Git Bash.

Project Overview

In this lesson, we will write a bash script that automates basic routines when setting up our development environment.

We will practice with a simple web development project, but you can implement the idea in other software development projects.

Introduction

A Bash Script is a file containing a sequence of workflow commands that are executed by the bash program. It allows you to store a series of workflow commands inside a file and execute the file when you want to perform those tasks.

These workflow commands may include, navigating to a directory, creating a directory with files or launching a process via the command line.

How to write Bash Scripts?

We need to follow the steps below to write a Bash Script.

  • First, we will create a file with the .sh extension

  • Next, we will write a series of workflow routines that we will manually perform

  • Lastly, we will provide execution permission for it

Create a bash script

We will use the Vim text editor to create our bash script.

Open your git bash application and enter the command below.

vim test-dev.sh

This command creates a test-dev.sh file if it does not exist and opens it in the Vim text editor.

Add commands to the script

To insert a command into our script in Vim, we need to click a or i to append or insert content to a file.

Adding the Shebang

Bash scripts start with a shebang statement that tells the computer to execute the file as a bash program. Shebang consists of bash # and bang ! followed by the bash shell path.

Add the code below to the first line of your bash script.

#!/bin/bash

Add workflow routines

Let's understand the typical workflow routines and how to write commands for each step.

Before working on a small web development project, you'll normally perform these tasks in setting up your development environment;

  • Step One - Navigate to the Desktop directory && Create a new directory for the project

  • Step Two - Go into the new directory && Create index.html, style.css, and main.js files

  • Step Three - Open the project in your code editor

Copy the following lines of code to your bash script.

cd Desktop/ && mkdir $1
cd $1 && touch index.html style.css main.js
code .

Here is a breakdown of what each line of code is doing.

cd Desktop/ && mkdir $1:

  • cd Desktop/: This changes the current directory to the Desktop directory.

  • &&: This logical operator ensures the second command is only executed if the first one is successful.

  • mkdir $1: This creates a new directory with the name passed as the first argument to the script ($1 represents the first argument). For example, if you run bash-script.sh myProject, $1 would be myProject, and this line would create a directory called myProject on your Desktop.

cd $1 && touch index.html style.css main.js:

  • cd $1: This changes the current directory to the newly created directory named $1.

  • touch index.html style.css main.js: This creates three empty files in the current directory: index.html, style.css, and main.js. The touch command is used to create new, empty files.

code .:

  • This opens the current directory in Visual Studio Code, allowing you to immediately start working on the newly created project files in the editor.

Save and make the file executable

Inside your Vim text editor, click esc and type :wq. This command writes the code to the file and quits the Vim program.

Run the command below in your terminal to make the bash script executable.

chmod +x test-dev.sh

Test and Run the bash script

Lastly, to execute our bash script we run the command below

./test-dev.sh project-name

The command above will create a project directory called project-name inside of the Desktop directory.

You should have something like this;

Desktop/ -
         project-name/ -
                         index.html
                         main.js
                         style.css

Conclusion

We've seen how to automate a basic workflow routine using bash scripting, we also looked at some bash commands and the Vim text editor.

As a command-line interpreter, Bash provides ease of use for developers to create scripts that automate workflows. There are other topics to cover if you want to learn more about Bash scripting.

What project setup will you implement next after reading this tutorial?

You can also connect with me on these platforms.

See you in the next tutorial. ๐Ÿ––

ย