Introduction to Bash Scripting – Part III

This is it! The Grand Finale of the Introduction to Bash Scripting course, we will be covering the creation of custom functions that can be called in the script. These functions can expect parameters or return updated values, and act according to whatever custom code you choose to run inside it.

We have reviewed the foundations of Bash Scripting in Part I and Part II of this series, which I recommend to read if you have not done so yet. The information reviewed in the previous parts of this article are now going to be applied from this point on, and the next articles will be the creation of different scripts to review the logic behind the code and a solution path to achieve the desired goal of said script. Now without further a due, let’s get our fingers dirty!

FUNCTION

You declare a function with the syntax function functionName() { #some code… }. After adding the function to the script file, you will be able to invoke it by simply calling the functionName in the script:

function sayHello() {
echo “Hello World”
}

# Calling the function sometime later in the code
sayHello

FUNCTION WITH PARAMETERS

When you are trying to pass some parameters to the function in order to iterate over it, you can use what is called “positional parameters”. These parameters are defined by the expression $n where n is the position of the input variable.

function greet() {
echo “Hello, I am $1 and I am $2”
}

# $1: “Isaac”
# $2: “36”
greet “Isaac” “36”

CREATE FOLDER AND WRITE TO A FILE

Now let’s take what we have learned so far and apply it into several examples of scripts that we will create later on. Today we will see our first example, where we will create a folder and write data to a file within this created folder:

#! /bin/bash

# Let’s define the function

function writeToFile() {

mkdir hello
touch “hello/world.txt”
echo “Hello World” >> “hello/world.txt”
echo “Created hello/world.txt”

}

# Now we validate if the file exists

if [ -f “hello/world.txt” ]
then
echo “hello/world.txt exists”
else
writeToFile
fi

CONCLUSION

The information shared so far should be enough to get a solid foundation (or at least a good cheat sheet to refer back to 😉) for creating Bash Scripts, however the only way to truly master this skill is to go ahead and try to create some scripts for yourself.

I recommend you go and try to spin up a new droplet in a VPS (Virtual Private Server) Service Provider such as DigitalOcean. Using my link you will receive a $50 credit that is good to deploy a small VPS with Ubuntu or another Linux distribution to use as a testing playground for quite some time. The smallest droplet costs $5 per month, so you will be good for up to 10 months before having to pay a single dime 😁. I will soon publish some articles on how to deploy your own testing server and implement some security measures and LAMP/LEMP stacks for web development.

Subscribe to my Newsletter to get notified when I drop any new article! Remember to also follow me in my social media accounts, comment in this post and share this guide with everyone! We’ll read us again soon.

Subscribe to my Newsletter

Exclusive offers, Industry insights and more!

Join 2,031 other subscribers