Enter your email address below and subscribe to our newsletter

Bash for Dummies #1: Create and Run Your First Hello World Script

Learn how to run your first Hello World program in Bash.

Share your love

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:

  • What is a bash script
  • Creating and running a dummy script
  • How to run script with bash interpreter
  • Writing Hello World script

So let’s start with the first one.

What is Bash scripting?

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.

Let’s write a dummy script

To create and run a shell script, you need to go through 3 steps:

  1. Create a file and lines that you want to execute.
  2. Make the file executable (or else the file will be treated as a normal text file)
  3. Execute the script

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
List files using ls command

Now, execute the pwd command which will tell you the current working directory:

pwd
Use the pwd command in linux

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
Write your first bash script

To save this file, follow two steps:

  • Press Ctrl + O and then press the Enter key to save the file.
  • Press Ctrl + X to exit from the nano editor.

Looks confusing? Here’s how to do it:

Save and exit from the nano editor

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.

Make file executable in bash

Now, you can check the permissions of the file using the ls command and it should reflect the execution permissions:

ls -la dummy.sh
Check file permissions to verify if the file is executable or not

The first part of the output is rwxrwx-x here’s a simple breakdown:

  • r – read permission
  • w – write permission
  • x – executable permission

In 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
Run your first bash script

As you can see, when I executed the script, it executed the ls and pwd commands one by one.

Now, let’s run a bash script (not a shell script)

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:

  • Use the bash command while executing the script
  • Use #!/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
Use bash interpreter via bash command

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:

Use bash interpreter in script

Run your first Hello World program

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
Write hello world in bash script

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
Run hello world script in bash

There you have it!

Bonus: Combining echo with other commands

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:

Use echo with other commands for bash

That looks like a real example of how you should be using the echo command in the bash script, right?

Next: Learn how to use Variables in Bash

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.

Share your love
Kabir
Kabir

A tech journalist whose life revolves around networks.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!