Introduction to Bash for Beginners

Bash is an automation powerhouse for UNIX based systems

Introduction to Bash for Beginners

Bash is an acronym of “Bourne-Again Shell”. It is a default command-line interpreter for UNIX and Linux based operating systems. In UNIX and Linux based operating systems, a terminal window is consist of a shell and Bash.

Bash is the powerhouse of these systems, which includes Linux distributions as well as MacOS among others. Learning bash can be super useful to perform automation. I’m someone who is always installing a new OS, and setting all my configs and programs manually each time can be super painful, but thanks to a few bash commands that I created I can fully automate the procedure.

Using bash I can install software, download configs from the net, set up configurations for my desktop, and much more.

Bash is super powerful and has lots of features. Today I’m going to present a few commands and statements that will help you automate a good deal of actions.

Bash shell scripting guide

For the bash shell scripting, we need to use the shell and the text editor. The extension of the shell scripting file is .sh. To create a new scripting file, create a text file, and save it with .sh file extension. I am using Ubuntu 20.04 for the demonstration and creation of Bash scripting files. The gedit is the default text editor for Ubuntu and many Linux based operating systems.

Let’s create a new file

Create file

To execute the bash scripting file, first we need to change the permissions of the file and make it executable. The permission are changed by chmod +x command. In order to make the bash scripting file executable, you have to open up your terminal and navigate to the folder or directory where the file is stored, and execute the following command.

chmod +x filename

Now let’s execute:

chmod +x command.

Give the file execution rights

Execution of bash script file:

The bash script file can be executed in two ways:

  1. bash filename
  2. ./filename

To execute the bash file, navigate to the folder or directory where the bash file is saved. In the first way of execution simply write bash and then the filename on the terminal and hit enter. The bash script file will be executed.

In the second way, write “./filename” on the terminal and press enter. We will use the second way for executing the bash file throughout this tutorial.

In this basic guide of bash scripting, we will cover the following things:

  1. Echo command
  2. Comments in bash scripting file
  3. ls command
  4. Variables declaration and usage
  5. Conditional statement
  6. Loops in Bash

Echo command

The echo command is the most fundamental and basic command in bash scripting. It is mainly used to print the text or output of the linux terminal. The echo command will print the text or data on the terminal whatever you will write. Let’s open our file and use the echo command to print some text on the terminal.


echo "Hello everyone."
echo "We are executing the echo command"

Output: The output is displayed on the terminal.

The echo command simply prints out the text on terminal.

Echo Sample Output

Comments in bash scripting file

Comments are the important part of computer programs. They are non-executable lines. Comments only enhance the readability of the code, and help to understand the purpose of our code or script. In bash scripting file, we can add the single line comments and multi-line comments. The single line comments start with ‘#’ symbol. The multi-line comments start with the single quote (‘) and : is used for adding lines of comments. Let’s see an example of comments in bash scripting.


# using the echo command
# This is the single line comment
echo "Hello everyone. This is the example of single line comment"
: '
This is the multi line comments
lets print another echo command
echo "Bash is intresting"

The ls command

The ls command is very basic shell command that you should be aware of. The ls command lists file and directory information in a file system. With various options, the ls command can be used. Let’s write the ls command in our file and execute it. The ls command can be executed directly on the terminal as well.


# using the ls command


ls Sample Output

Variables declaration and usage

Variables are declared to store the data or some information. Variables are the important aspect of any programming language. We can store some values or information in variables and later on we can use it. The declaration and usage of variables is very simple in bash. Variables are simply declared by writing the variable name. While accessing or using the variable name, we write ‘$’ symbol with the variable.

Let’s declare the variable and use them in our bash scripting file.


#declaring a variable
VAR="Welcome to the bash scripting"
#using the variable
echo $VAR

#declaring the num1 variable
#declaring the num2 variable
#calculating the sum and storing it in num3 variable
#printing the sum
echo "The sum is:$num3"


Vars Sample Output

Conditional statement

The conditional statements in Bash are used for decision making. In conditional statements a specific condition is evaluated. If the condition is true, then a certain block of code is executed. Otherwise, a second block of code is executed.

Like other programming languages, in Bash, the if statement is used to evaluate a condition. We can evaluate one or multiple conditions inside the if block by using OR and AND operators. The if block starts with if keyword and ends with the fi keyword in Bash. If a certain condition is false, then the else block is executed.

Let’s see the execution of conditional statements in Bash. We have declared a variable. We are evaluating the value of variable and making decision based on the variable value. The –lt is used to make the less than comparison. For the comparison we can use, -gt for greater than, and -eq for equality.


#declaring a variable
if [ $VAR -lt 10 ];
	echo "The number is less than 10"
	echo "The number is equal or greater than 10"

Output The output shows that “The number is less than 10” because the value of variable is 7, which is essentially less than 10. In this given example the if condition is executed.

Conditional Sample Output

Similarly, we can used OR and AND condition inside the if block to evaluate the multiple conditions. The double pipe sign (||) is used to specify the OR logic and && is used to define the and operator logic.

Let’s see an example of OR logic in Bash.


#declaring a variable
#using OR operator in if condition
if [[ ( $VAR -gt 5 ) || ( $VAR -eq 7 ) ]]
	echo "The number is greater than 5 or equal to 7"
	echo "The number is not greater than 5 nor equal to 7"


Conditional with OR Sample Output

Now let’s implement the AND operator inside if condition. In the AND operator, both the conditions should be true.


#declaring a variable
#using OR operator in if condition
if [[ ( $VAR -gt 5 ) && ( $VAR -lt 10 ) ]]
	echo "Both the conditions satisfy"
	echo "Both the conditions do not satisfy"


Conditional with AND Sample Output

Else if statement in Bash

The else if condition or statement is used to evaluate the multiple conditions. To define the else if condition in Bash, we use elif keyword.

Let’s see an example of else if statement in Bash. We will take the input from user.

The read keyword is used to take input from the user.


echo "Enter a number"
read num
#declaring if condition
if [ $num -gt 10 ];
	echo "The number is greater than 10"
elif [ $num -eq 10 ];
	echo "The number is 10."
elif [ $num -lt 10 ];
	echo "The number is less than 10"

	echo "Invalid number"


Conditional with ELSE Sample Output

This is how the conditional statements work in Bash.

Loops in Bash

The loops in Bash are used to execute the same block of code repeatedly. The while loop and for loop are the most commonly used loops in Bash.

We will see the examples of while loop and for loop in this section.

While loop in Bash

The while loop is used to execute the code repeatedly. It evaluates the condition, and continue executing until the test condition is false.

The syntax of while loop in Bash is as follows:

while [ test_condition ]
	statements or commands

If the test_condition is true, then do block is executed.

Let’s see an example of while loop. The loop continue execution until the value of variable is less than 10. Inside the do block, we are incrementing the value of variable by one on each iteration.


#declaring a variable
while [ $VAR -le 10 ]
       echo "The vale is: $VAR"
       #Incrementing the variable by 1
       (( VAR++ ))


While loop Sample Output

The loop terminated when the condition becomes false.

For loop in Bash

The for loop is also used to perform a task or execute the same block of code repeatedly. The syntax of for loop is Bash is follows:

for (( i=i; i>10; i++))

The i is a variable here.

Let’s see an example of the for loop. In the given example, we are incrementing the value of the variable and printing it on each iteration.


#declaring the for loop
for (( i=0; i<10; i++ ))
  #printing the value of variable
  echo -n "$i "

echo "\n"


For loop Sample Output


Bash is the default interpreter for the UNIX and Linux based operating systems. Bash executes the commands that we write on the terminal window. The commands can be written and saved in a bash (.sh) file and can be executed from the file as well.

In future articles, we will explore more in detail operations and commands we can use to interact with the terminal.

Thanks for reading!

What's Your Reaction?