Bash Scripting (P-1)

Bash Scripting (P-1)

Hi there !

So in my last article, I mentioned that the next thing I would be looking into is Bash Scripting. Normally I would want to finish gathering my knowledge about a topic before I start recapping what I learnt by writing these articles, but as I dived in, I discovered bash scripting is a lot. Even if I wanted to, I can’t possibly summarize the whole of it in one article. So I decided to recap as I go on. I might just not publish these blogs until I finish the whole thing. LOL

Let’s dive in !.

Bash Scripting ? Why am I looking into this ?

So, basically, I have a guide I am following and the first segments of the guide involves getting familiar with linux systems. One of the prerequisites includes getting familiar with bash scripting, so here I am.

During my course of learning, I found out that you can use a bash script to simply automate tasks you perform on a regular basis on your computer system. For instance, you could write a simple bash script to run a backup of your system, or move a couple of files from one directory to the other. There are many other boring tasks that you might want to automate, so learning how to write a bash script might be making your life easier in the long run.

Bash Scripting ? What is this ?

Bash stands for "Bourne Again SHell”, which is a type of interpreter that processes shell commands. A bash script is just a file that contains some text (mostly commands) that would be processed by Bash and would be executed to perform some tasks on your computer.

Creating a bash script.

Like I mentioned earlier, a bash script is a file, so you might just want to start by creating a file with any name you would like. If you are already familiar with any programming language, you would ask what extension a bash script uses, as you are probably used to adding a .py to signify a python file, a .js or .ts to signify a javascript or typescript file and the likes. When I started, I usually just created a .sh file. Later on, I discovered that the file extension does not really matter for bash scripts. The only thing that matters is that the file should begin with a “shebang” / "sha-bang" / "hashbang" or whatever else you might choose to call it.

   #!/bin/bash

and it should be an executable file.

So go ahead, and create any file on your system. To be fancier, open a terminal right now and type this command

    touch hello.sh

where hello is the name of the file you want to create. This command will create a file hello.sh in the current directory you are in.

Go ahead and open this file in whatever text editor you use (notepad, vim or even vs code).

the first line of this file should have the "shebang"

   #!/bin/bash

Now to the next condition about making a file executable.

First, you can view the details of all items in that directory.

   ls -al

The command above will list the permission access details of all files in that directory. You would notice something like -rw-r--r--.

This can be split into three sections by an hyphen -rw, -r-, -r-

  • The first section represents the permissions of the owner of the file. rw means the owner can read and write to the file.
  • The next section represents the permission details of other groups on the system you are using. in this case r-. This means other groups can only read this file.
  • The last section represents the permission details of the public.

To make a script executable, use the command

   chmod +x [NAME_OF_SCRIPT_FILE].sh

Now that the script is executable, let us test it out by writing some commands in the file.

     #!/bin/bash

     echo "Hello World"

You can copy the above text and paste it in your bash script. I know I have not talked about what the echo in that script is meant to be, but it is basically a built-in command that prints out whatever is passed to it. Just like the print and console.log in python and javascript respectively.

  • Running your script.

This has to be the easiest part. As long as you are in the directory where your script is, all you need to do is call the name of the script and press ENTER on your keyboard ..

    ./hello.sh

That should execute your bash script . Congratulations on running your first bash script 🎉.

Now that I have talked about how running a bash script goes, I think we should dive into the main things involved in writing a bash script.

Basics

Before I get into this, I would like to point out one thing that almost drove me nuts when I got started with this.

  • Spacing: I was so angry when I found out that what stopped my script from running was just a space I left in between an equal to sign and a string when i was trying to declare a variable. This space issue made me question my life decisions. LOL. Later on, I figured that spacing and quotations are really important as far as bash scripting is concerned. You might want to always take note of these little things so you do not run into issues later on.

Variables

Declaring a variable: Unlike some other scripting languages where you have to declare variables using "CONST, LET or VAR", in bash scripting, you can simply declare a variable using the = sign.

Example:

    name=Goldin
    age=45
    favourite_drink=Cocktail
    FULL_NAME="Samuel Olamide"

Reading a variable : To read the value of a variable we prepend its name with a dollar sign "$" anywhere we would like use that variable in our script. Before Bash runs a line of our script, it first checks to see if any variable names are present. If there is a variable, it replaces the variable name with its value.

Important things to note.

  • You would notice that I did not leave any spaces between the name of the variable and the value of the variable. This is actually important.
  • You would also notice that i used " " for values that is more than a word. This is also important so that you do not run into errors.
  • Variables in bash scripting is always globally scoped. This means a variable can be easily accessible from anywhere within a bash script.

I think I would stop here for now before this gets too long. In my next scribble, I would most likely still be hammering on the basics of bash scripting. Till then, Ciao !!!