How to start writing bash scripts

Table of Contents

Introduction

Firstly open up the terminal, change to a suitable directory to store the script file, and then use the touch command to create the bash script file.

This is why I have this guide on how to write bash scripts to act as a way to refresh my memory on using bash.

Creating a basic script

Firstly open up the terminal, change to a suitable directory to store the script file, and then use the touch command to create the bash script file.

touch script.sh

The script file needs to be executable so it can run, this can be done by changing the file permission using the chmod command.

 sudo chmod u+x script.sh

Use a text editor to open up the file such as Nano or Vim.

nano script.sh

Copy and paste the following code.

  • The first line sets the script to run via bash
  • The second line prints out the text “Hello World!”
#!/bin/bash
echo "Hello World!"

Exit out of the text editor.

Now run the script.

./script.sh

After running you should see the Hello World text output.

Passing arguments

Edit the script file created in the above basic script example.

Replace the content with the following.

#!/bin/bash
echo "Argument: $1"

To pass in the argument call the script with the text entered in after the script name.

./script.sh HelloWorld

The following text will be output

Argument: HelloWorld

Re-open the script.sh file in a text editor and replace the content with the following.

This script will test passing in multiple arguments.

#!/bin/bash
echo "Arguments: $1, $2"

Re-run the script again but this time pass two arguments.

./script.sh First Second

You will see those two arguments in the output.

Arguments: First, Second

Conditional statements

This section covers conditional code such as if statements, so code can be run depending on weather conditions, have been validated.

Example 1

This example will take an argument and output text depending on whether an argument was passed into the bash script.

#!/bin/bash

if [ "$1" ]
then
    text=$1
else
    text="Hello World"
fi

echo $text

Execute the script with no argument.

./script.sh

As the argument is blank then the default text will be output.

Hello World

This time pass an argument representing some text.

./script.sh "This text will be output"

As the argument is set the text will be output.

This text will be output

Example 2

This example will request to input an answer to the question asked, depending on the answer given a different output is shown.

#!/bin/bash

echo -e "Guess the programming language"
read lang

if [ $lang = "PHP" ]
then
    text="PHP is correct"
else
    text="${lang} is not correct"
fi

echo $text

After copying and pasting the above code to the bash file then run the script.

./script.sh

After executing the script you will get the following prompt.

Guess my main programming language

Entering the text “PHP” will return the following.

PHP is correct

Anything else such as “JavaScript” will return the following.

JavaScript is not correct

Loops

This script will set up an array of strings, each string representing a programming language. The loop will go through each data in the loop and output it.

#!/bin/bash

langList=("PHP" "JavaScript" "Go" "Python")

for lang in ${langList[@]}
do
  echo $lang
done

The above script will output the language on each line.

PHP
JavaScript
Go
Python

Here is another example except this script loops through a string rather than an array, the output is the same as above.

#!/bin/bash

langList="PHP JavaScript Go Python"

for lang in $langList
do
  echo $lang
done

Conclusion

After going through this guide you should have enough knowledge to write basic bash scripts, this also serves as a good refresher to bash scripts as well. The next step you should take is to practice thinking about what kind of scripts you would need and write them.

A great example is where the script will read your MySQL databases, run the SQL dump command to back up the database, and store it in a safe location.