Newsletter Subscribe
Enter your email address below and subscribe to our newsletter
If you want to become a hacker, cloud engineer or pretty much everything in IT, then learning bash scripting will give you an edge over others.
Bash scripting is mainly used to automate operations in servers. These are those operations that would take years if you were to perform them manually.
It is similar to automating your air conditioner after a certain temperature but with Linux, you can get more creative. Sure, other operating systems also allow you to automate operations but with Linux, you go one step further.
This is the reason why we are starting a series on bash scripting where I will walk you through all the basics you will ever need to get started with the bash scripting.
In the first part of this series, I will walk you through the following:
So let’s start with the first one.
Table of Contents
A bash script is simply a text file that contains one or more commands. When the script is executed, the commands are run sequentially as if you entered them manually in the terminal.
Let’s suppose you need to execute 200 commands. Sure, if you had to do it once, writing a script wouldn’t make any sense but if you had to do it again and again, writing a script is a better idea.
When you run a script, it executes every line one by one. So it is like a sequential way of executing commands. The best part is you can also automate the execution of scripts based on specific scenarios such as executing a script while the system boots.
To create and run a shell script, you need to go through 3 steps:
To write a script, you need a text editor. While anything like a notepad would also work. I encourage you to perform everything in a terminal window. So I will be using a nano text editor.
To give you a better context of script work, open your terminal and execute the ls
command to list files and directories in the current directory:
ls
Now, execute the pwd
command which will tell you the current working directory:
pwd
Now, think you want to execute these two commands at once. Sure, you can use the &
operator to run multiple commands but it will get more complex as the chain of commands increases.
Here’s where the idea of the bash scripting comes in.
First, open your terminal and use the nano command as shown here to create your first bash script named dummy.sh
:
nano dummy.sh
It will create and open the dummy.sh
file. Now, write those two commands in the first two lines as we intend to execute them one by one:
ls
pwd
To save this file, follow two steps:
Ctrl + O
and then press the Enter
key to save the file. Ctrl + X
to exit from the nano editor.Looks confusing? Here’s how to do it:
But you can not run the script yet!
Linux will treat the file as a normal text file. So how do we change it to a script which can be executed? Well, the answer is simple.
You need to make the file executable. To make the file executable, you need to use the chmod
command as shown here:
chmod +x dummy.sh
Here, I’ve used the +x
flag with the chmod command which will add permission to execute the file.
Now, you can check the permissions of the file using the ls
command and it should reflect the execution permissions:
ls -la dummy.sh
The first part of the output is rwxrwx-x
here’s a simple breakdown:
r
– read permission w
– write permissionx
– executable permissionIn simple terms, it suggests that the file is not ready to be executed.
To execute the file, all you have to do is append ./
before the filename and it should be executed:
./dummy.sh
As you can see, when I executed the script, it executed the ls and pwd commands one by one.
Previously, I explained how you can run a script but I didn’t mention bash
anywhere in the script or during execution.
It was a shell execution, not a bash execution.
Linux can have multiple shells. Sure, the basic syntax remains the same for the most part, specifying the bash interpreter lets you use more features.
There are two ways you can use the bash interpreter:
bash
command while executing the script #!/bin/bash
as a first line in your bash script (a standard practice)If you want to use the bash interpreter using the bash
command, then here’s what the syntax looks like:
bash <script-name>
The script I created earlier was named dummy.sh
so if I wanted to use the bash interpreter using the bash
command, then the command would look like this:
bash dummy.sh
But ideally, you are supposed to mention the interpreter in the script itself. That’s where the second method comes in.
To mention the interpreter in the script, you need to add the !#/bin/bash
line at the beginning of the script. To do so, first open the script with the preferred text editor. I’m going with the nano editor:
nano dummy.sh
Now, add the #!/bin/bash
line at the beginning of the file as shown here:
So far, I’ve mentioned how you can create a script, make it executable, add a bash interpreter and how to run it. Now, in this section, I will summarise everything briefly, so bear with me and execute your first Hello World script.
First, open your terminal and use the nano editor to create and open a new script:
nano hello.sh
Now, write/paste the following lines in the file which will allow you to run a Hello World program:
#!/bin/bash
echo Hello World
Here, I’ve used the echo
command in the 2nd line which allows you to print text and have appended the “Hello World” text which I want to be printed at the time of execution.
Next, save changes by pressing Ctrl + O
and hitting the Enter
key. Finally, press Ctrl + X
to close the nano editor.
Now, make the file executable using the chmod
command to make the file executable:
chmod +x hello.sh
Finally, you can run the script as shown here:
./hello.sh
There you have it!
While using the echo command to print normal text is good but I’d like to show you a more practical way of using echo with other commands.
The idea is to print the text along with the other command’s output through the echo command to get a more meaningful command.
To use commands along with the echo command you need to enclose the command in $(command_name)
syntax. This is known as command substitution. The command inside the parentheses will be executed first, and its output will be captured and inserted into the echo command.
Let me give you an example of a script which uses basic commands to tell the system information:
#!/bin/bash
echo "Current Date: $(date)"
echo "Current User: $(whoami)"
echo "Current Directory: $(pwd)"
echo "Disk Usage:"
echo "$(df -h | grep /dev/)"
As you can see, I have used command substitution multiple times and when you execute this script, the output will look like this:
That looks like a real example of how you should be using the echo command in the bash script, right?
In the next part of the Bash for Dummies series, I will explain how you can use variables in Bash from basic to advanced level. If you have any other ideas, leave us a comment.