What is Linux Bash Scripting

What is Linux Bash Scripting? Well, Bash is a command line interpreter. And Linux Bash Scripting is for executing a series of commands in Linux Shell. It may seem intimidating at first, but we would look at a few Linux bash scripting examples and see that it’s not that difficult.   

Prerequisite

Table of Content

  • Basic Hello World Bash Script  
  • Variables in Bash Script 
  • Reading User Input in Bash Scripting  
  • Arrays in Bash Scripting  
  • IF / Else Statements in Bash Scripting  
  • Functions in Bash Scripting  
  • Loops in Bash Scripting 
  • Practice Questions

Linux Bash Scripting

What is Linux bash scripting? 

Bash scripting is basically a shell script which is actually a program that is executed by a command line interpreter. They are a group of many commands to be run sequentially to perform a task. They are all the basic Linux commands that we already know and use in Linux Terminal.  

Let’s see some Linux bash scripting examples. 

1. Basic Hello World Bash Script 

First, we need to create a shell script file. Execute the following command to create and open a shell script with ‘nano’ text editor. 

Now paste the below code inside it. 

Explanation: 

  • In line 1, we have to mention the script interpreter after the shebang (#!). Since we are performing bash scripting, the interpreter is at /bin/bash. 
  • In line 2, we are creating a variable myText and then insert a string value in it. 
  • In line 5, we are printing the value of the variable out in the console.  

By default, a shell script is not executable because it does not have the rights yet. Therefore, we need to give a script.sh executable rights before we can run our bash script. You can find more details for Permissions and rights for Linux here. 

To quickly give executable rights, exit the nano text editor and execute the below command. 

Now it’s time to finally run the bash script. To execute the bash script, enter the below command where the script.sh file is located. 

OUTPUT

Hello World

2. Variables in Bash Script 

Explanation: 

  • Line 1 is where you mention your script interpreter (/bin/bash). 
  • Line 2 is where we are creating a variable named var1 and giving it a value. 
  • Line 3 is simply printing out the variable’s value. 

 

Execute the file and the output should be as follows: 

PROGRAMATICALLY

Global vs Local variable 

It follows the same concept as in any other programming language. A global variable is identifiable and accessible throughout the bash script. Whereas local variables are only accessible in their defined scope, such as inside a function. 

Explanation: 

  • The global variable is defined outside of the function and the local variable is defined inside.  
  • When the script is executed, the global variable was called first and then afterward the testFunction was called which printed the local variable.  

The output should look like this: 

my global variable
my local variable

3. Reading User Input in Bash Scripting 

It’s always a great idea to make your script dynamic. The ‘read’ command is used to take in user input in Bash Scripting. Copy the code below and execute the Bash Script. The output is also shown in the image below as well. 

OUTPUT

User Input for Bash Scripting in Linux
User Input for Bash Scripting in Linux

4. Arrays in Bash Scripting 

In the below Linux Bash Scripting example, we are initializing an array and then iterate through it using For Loops and print the elements as output. 

There are 2 methods to initialize an array in Bash Scripting. Let’s see both of them.

After initializing the array, let’s see how to print them out in the console using loops. Copy the code below and execute the Bash Script.

OUTPUT

Debian Linux
Redhat Linux
Ubuntu Linux
Mint Linux

5. IF / Else Statements in Bash Scripting 

In IF/Else statements, you would need to specify when you want to close the statement by writing FI at the end.  

The conditions have their unique annotations, whose chart I’ve mentioned below the code. Other than that, it’s very straightforward.  

Bash Script: Mathematical Operators 

-lt 

< 

-gt 

> 

-le 

<= 

-ge 

>= 

-eq 

== 

-ne 

!= 

Bash Script: String Operators 

= 

equal 

!= 

Not equal 

< 

Less than 

> 

Greater than 

-n var1 

string variable var1 is not empty 

-z var1 

string variable var1 is empty 

6. Functions in Bash Scripting 

We have already seen a basic example of functions in bash Scripting. Let’s look at a few more examples of it. Copy the code and execute the bash script. 

OUTPUT

You called Function 1
Hello World!
testFunction_1

7. Loops in Bash Scripting 

In LOOPS, you would need to specify when you want to close the loop by writing done at the end.  

The below code is basic FOR Loop which prints all the files present in /etc/ directory. 

FOR LOOP in Bash Scripting 

OUTPUT


sysctl.conf
sysctl.d
systemd
terminfo
timezone
tmpfiles.d
ucf.conf
update-motd.d
vim
xattr.conf
xdg

WHILE LOOP in Bash Scripting 

OUTPUT

counter = 0
counter = 1
counter = 2
counter = 3
counter = 4
counter = 5
counter = 6
counter = 7
counter = 8
counter = 9

Loop through an Array in Bash Scripting  

Following is the same example that we saw in the Arrays in Bash Scripting section.  

OUTPUT

Debian Linux
Redhat Linux
Ubuntu Linux
Mint Linux

FOR EACH LOOP

Let’s print out the same array list using a ‘For Each’ style loop. Copy the code below:

Debian Linux
Redhat Linux
Ubuntu Linux
Mint Linux

8. Bash Scripting Sample practice Questions

Q1. Take an Integer user-input ‘n’. Create an array of random words and only print those words out which are longer than the length of ‘n’.

Q2. Take 5 integers as user input and store it in an array and sum all the numbers using loops. 

Q3. Print out any Math table using While Loop.

Q4. Create a Function that takes in an integer parameter and checks which range it falls in. 
1 – 50
50 – 100
100 – 1000

And that’s a wrap! 

These commands will only work on a LINUX terminal. And a common way to run Linux with Windows is to start a Virtual Machine using VMware. 

I hope this article helped you learn about What is Linux Bash Scripting. You may also want to learn Basic Linux commands hands-on or Users, Groups, and Permissions in Linux. Please like this article and leave your reviews in the comment section below. 

Have a great one!