Bash Scripting: The Skill for Software Engineers

Bash Scripting: The Skill for Software Engineers

As a Software Engineer/Developer, it is inevitable to write scripts, be it for task automation, data analysis, data visualization, etc. These scripts can be written in different programming languages such as Python, PHP, Go, and the rest. However, it is important to know, write and understand bash scripts in this modern day.

What is Bash scripting?

This can be thought of as writing single or multiple Linux commands into a file, saving the file, and having our shell execute that file.

Here is a simple example of a Hello World bash script. images.png

Why Learn Bash Scripting?

As a Software Engineer/Developer in 2022, you interact with your machine mostly through the terminal, and you can only do that through writing Linux commands. Taking this a step further will definitely help level up your skills.

Advantages of Bash Scripting

We use bash scripts mainly for automation, i.e when we have reoccurring tasks or commands we want to automate, that is the area where bash scripting shines the most. Nonetheless, there are still other advantages of bash scripting which will be listed below.

  • Compared to writing individual commands in your shell it is faster and quicker
  • It can be used for the customization of administrative tasks
  • It is also used for creating your personal tools/utilities
  • It is easy to use

Writing Your First Bash Script

Now that we have a little understanding of what bash scripting is, and why bash scripting is important, let us write a simple bash script to log out Hello World to our terminal.

First, we create a bash file using.

touch hello.sh

We use the file extension .sh for bash files.

We need to know that every bash script always starts with a shebang !#. This is used to tell the operating system which interpreter to use to parse the rest of our script. After the shebang, we specify which interpreter to use. For us to use our bash interpreter all we need to do is specify the path to our bash interpreter, if you don't know that, open a terminal window and type in which bash and then hit enter.

Next, we open our bash file with any text editor of our choice and add our shebang followed by the interpreter. Like this

!#/usr/bin/bash

This is known as a shebang interpreter derivative.

Next, we add

echo "Hello World"

Finally, our script should look like this

!#/usr/bin/bash
echo "Hello World"

We have written our first bash script. To execute this in our terminal we first need to change the permissions on the file so we can run it without sudo.

Type this in your terminal and hit enter

chmod +x hello.sh

Now, we can run our bash script. Type this in your terminal and hit enter

./hello.sh

You should see the output Hello World in your terminal.

In conclusion, if you still want to know more about bash scripting, or you have decided to pick up bash scripting to add to your skills as a developer/engineer, here are some good resources to help you get started.

Have a great time learning. Cheers!!