Bash Programming Introduction A Beginner's Guide




Working on Linux operating system seems cool to everyone. If you are in love with computers and programming languages, you might wanna not miss working in the Linux operating system.

What makes Linux a cool operating system is, The command-line interface or in short CLI. When I started learning the Linux operating system, I was so interested in CLI that I, want to do each and every task possible through writing commands in CLI, even the "poweroff" command. That was fun.

In this post, I will give a short introduction to the BASH scripting language, the common programming language used in every shell of any Linux operating system.

Writing a Bash script will make your task so simple, and make your day easy while working on the Linux operating system. So, Let's jump...

Every programming language follows a particular syntax and so is the Bash. Bash follows some unique syntax style that you should keep in mind while writing a script.

Every Bash script starts with the following line:

#!/bin/bash
This line tells the shell that executes this in Bash programming style. This is known as shebang.

Now, Let's talk about variable assignments.

Variables In Bash

Bash follows a very disturbing way of variable assignment. Suppose, I want to store 5 in a variable x, then I have to write

x=5
Notice that, There should not be any space left in between the = sing. If you put space and write it like x = 5 then you will get an error.

Look At the below screenshot:



When I put space while assignment, I get an error x: command not found!

I will explain why this happens, but now just digest the fact that we should not use space while assigning values to variables.

Let's look forward to how we can show an output.  Like every programming language has an print() function, or printf() or System.out.println(),  Bash has its own command of showing us the output.

echo

use echo to print something to the standard output:

echo Hello Folks How Are You
echo Bash is simple and sweet

Output

Hello Folks How Are You
Bash is simple and sweet

Observe, like other programming languages we did not use any double quotes here. Double and single quotes have different notions in bash. Let's look at them in more detail, as we have covered the variables and echo concepts.

Double Quotes Vs Single Quotes

A simple terminal in the Linux operating system looks something like this:



Here, you write your commands like "cd" or "mkdir". So, when you write a Bash script in a file, and execute them, each instruction is executed in this terminal. Now, what is the role of double quotes?

Let's write a script to understand this:

Suppose I create a variable in this terminal x="ls -l"
Now, we know that ls -l command is used to show the detailed contents of a directory.

x="ls -l"




Now, to use the variable x we created above, we have to write $x always remember to put the $ sign before the name of the variable when you are using it.


Output

(base) soul@soul-Lenovo-ideapad-330S-14IKB:~$ ls -l
total 10608
-rw-rw-r--  1 soul soul        0 Jun 30 12:52 2
-rwxrwxr-x  1 soul soul     1010 Jun 17 19:54 ABoardGame.py
drwxrwxr-x 26 soul soul     4096 May 30 22:57 anaconda3
-rwxrwxr-x  1 soul soul     2748 Jun 13 19:04 ANewHope.py
drwxr-xr-x  5 soul soul     4096 Jun 30 20:16 Desktop
drwxr-xr-x  2 soul soul     4096 May 30 00:03 Documents
drwxrwxr-x  6 soul soul     4096 Jun 30 20:56 Downloads
-rwxrw-r--  1 soul soul 10766281 May 21 23:34 kite-installer
-rwxrwxr-x  1 soul soul      502 Jun 14 21:06 matrixRotate.py
drwxr-xr-x  2 soul soul     4096 May 30 00:03 Music
drwxr-xr-x  2 soul soul     4096 Jun 30 13:34 Pictures
drwxr-xr-x  2 soul soul     4096 May 30 00:03 Public
-rwxrwxr-x  1 soul soul      919 Jun 18 11:53 rotation.py
drwxr-xr-x  4 soul soul     4096 Jun  4 19:08 snap
drwxrwxr-x  2 soul soul     4096 Jun 25 20:23 SOFTWARES
-rwxrwxr-x  1 soul soul      432 Jun 19 13:00 stringChange.py
drwxr-xr-x  2 soul soul     4096 May 30 00:03 Templates
-rwxrwxrwx  1 soul soul      124 Jun  6 10:57 t.py
-rwxrwxrwx  1 soul soul      129 Jun  6 10:54 try.py
-rw-rw-r--  1 soul soul     3771 Jun 28 22:07 Untitled1.ipynb
-rwxrwxr-x  1 soul soul      588 Jun 23 18:00 untitled1.py
-rwxrwxr-x  1 soul soul      919 Jun 15 12:41 untitled2.py
-rwxrwxr-x  1 soul soul      306 Jun 23 18:00 untitled3.py
-rw-rw-r--  1 soul soul     1412 Jun  7 20:03 Untitled.ipynb
drwxr-xr-x  2 soul soul     4096 May 30 00:03 Videos

Observe, when I write the $x, it simply put the value in the terminal as ls -l, and the terminal executed the ls -l command!

That's the beauty of Bash.

But, what if I pass this variable in a message? or what if I write like this "$x"? Let's observe...




Output

(base) soul@soul-Lenovo-ideapad-330S-14IKB:~$ echo "This is the value of $x"
This is the value of ls -l
Now, what if I write that same message in single quotes?




In short single quotes do not unpack the variable, and act like a string, but double quotes will unpack your variable to the terminal.

a="ls -l"
a $HOME

output

ls -l /home/soul

But,

a '$HOME'

Output

ls -l $HOME
Now, That we have talked about double and single quotes, Let's talk about how we can protect our variables.

Enclosing variables in brackets

First, why we need to enclose our variables in brackets? Suppose you store a username in a variable "user" and you do want to do the following:

user="soul009"
$user@gmail.com
This will be a big blunder, right? The interpreter will assume this as a variable of name user@gmail.com
Instead, we can use the concept of enclosing variables.

user="soul009"
${user}@gmail.com

The terminal will interpret it as
Output

soul009@gmail.com

read

We can take input from stdin or user with the read command. Let's write a short code to illustrate the working of read command:

#!/bin/bash
echo "Enter Your Message"
read msg
echo "Your Message is $msg"

Output

Enter Your Message
hey how are you
Your Message is hey how are you

Now, Let's end this topic with a very interesting Bash script the newbies will love to write. At least I was feeling very happy after writing this noob code.

The below code will create a simple folder in the Desktop directory of your Linux machine, We will ask the user to input the name of the folder he wants to create.

#!/bin/bash
echo "Welcome To My First Bash Script"
echo
echo "--------------------------------------------------------"
echo
echo "Please Enter The Name Of Folder You Want To Create"
echo
read name
mkdir $HOME/Desktop/$name
echo "File Is created!!!"

We discussed some very important and basic topics required to start complex scripting in bash. I will end this post here cause next we will look at how to write if-else, while and for loops in Bash.

Looping and conditional statements require an understanding of the basics like how to enclose your variables, the difference between double and single quotes. So Revise what you read and jump to the below-given link whenever you are ready to understand conditional and looping statements in Bash.


Do Follow the blog, share, subscribe if you like.
Thank you!

Post a Comment

1 Comments