Introduction to Bash for Beginners
Bash is an automation powerhouse for UNIX based systems
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
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 myscript.sh.
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 myscript.sh command.
Execution of bash script file:
The bash script file can be executed in two ways:
- bash filename
- ./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:
- Echo command
- Comments in bash scripting file
- ls command
- Variables declaration and usage
- Conditional statement
- 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 myscript.sh file and use the echo command to print some text on the terminal.
#!/bin/bash
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.
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.
#!/bin/bash
# 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 myscript.sh file and execute it. The ls command can be executed directly on the terminal as well.
#!/bin/bash
# using the ls command
ls
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.
#!/bin/bash
#declaring a variable
VAR="Welcome to the bash scripting"
#using the variable
echo $VAR
#declaring the num1 variable
num1=10
#declaring the num2 variable
num2=20
#calculating the sum and storing it in num3 variable
num3=$(($num1+$num2))
#printing the sum
echo "The sum is:$num3"
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.
#!/bin/bash
#declaring a variable
VAR=7
if [ $VAR -lt 10 ];
then
echo "The number is less than 10"
else
echo "The number is equal or greater than 10"
fi
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.
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.
#!/bin/bash
#declaring a variable
VAR=7
#using OR operator in if condition
if [[ ( $VAR -gt 5 ) || ( $VAR -eq 7 ) ]]
then
echo "The number is greater than 5 or equal to 7"
else
echo "The number is not greater than 5 nor equal to 7"
fi
Output
Now let’s implement the AND
operator inside if condition. In the AND
operator, both the conditions should be true.
#!/bin/bash
#declaring a variable
VAR=7
#using OR operator in if condition
if [[ ( $VAR -gt 5 ) && ( $VAR -lt 10 ) ]]
then
echo "Both the conditions satisfy"
else
echo "Both the conditions do not satisfy"
fi
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.
#!/bin/bash
echo "Enter a number"
read num
#declaring if condition
if [ $num -gt 10 ];
then
echo "The number is greater than 10"
elif [ $num -eq 10 ];
then
echo "The number is 10."
elif [ $num -lt 10 ];
then
echo "The number is less than 10"
else
echo "Invalid number"
fi
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 ]
do
statements or commands
done
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.
#!/bin/bash
#declaring a variable
VAR=1
while [ $VAR -le 10 ]
do
echo "The vale is: $VAR"
#Incrementing the variable by 1
(( VAR++ ))
done
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++))
do
done
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.
#!/bin/bash
#declaring the for loop
for (( i=0; i<10; i++ ))
do
#printing the value of variable
echo -n "$i "
done
echo "\n"
Output
Conclusion
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!