Bash Script: Hello World Example

When getting started with a new scripting or programming language, such as Bash scripting on Linux, the first thing a user learns to create is a Hello World script.

This serves as a basic introduction into Bash scripts, and gives you a simple idea of how a script is formatted in Bash. In this tutorial, we will take you through the steps to create your first Hello World Bash script on a Linux system.

It does not matter which Linux distro you are running, and you do not need any previous experience to follow along with the steps below. Let’s get started!

In this tutorial you will learn:

  • How to create a Hello World Bash script in Linux
  • How to give execute permissions to Bash script
  • How to execute Bash script
Execution of a Hello World Bash script on Linux
Execution of a Hello World Bash script on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Bash shell (installed by default)
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Bash Script: Hello World Example



Twitter icon Follow LinuxConfig.org on Twitter for the latest tips and tricks about Linux!


Follow along with the steps below to create your first Hello World Bash script in Linux.

  1. The first thing we will need to do is open the command line terminal. This should be easy to find on which ever distro you are running. If you are running the GNOME desktop environment on Ubuntu, then the keyboard shortcut to open a terminal is Ctrl + Alt + T.
    We have opened the command line terminal on our Linux system
    We have opened the command line terminal on our Linux system
  2. The next thing we will do is create a new file and open it in a text editor. For new users, the most recommended text editor to use on the command line would be nano. There are plenty of Linux veterans that also prefer to use this text editor as well. Execute the following command to open a new file named hello-world.sh in the nano text editor.
    $ nano hello-world.sh
    
    NOTE
    Although it can vary, Bash scripts usually use the .sh file extension. If you see a file with this extension on your system, you can bet that it is a Bash script.
  3. Inside of this file, paste the following text.
    #!/bin/bash
    
    echo "Hello World!"
    Our Hello World script inside of the nano text editor
    Our Hello World script inside of the nano text editor

    The #! on the first line is called a shebang, and tells the script where it can find the shell that it should be using to interpret this file. On Linux systems, the Bash shell is located at /bin/bash.

  4. To save and exit this file, press Ctrl + X on your keyboard. The nano editor will ask you if you want to save the changes, so hit Y on your keyboard. It will then ask you for a file name, which should already be filled out as hello-world.sh, so simply press Enter to finish saving the file.

    Saving the Hello World Bash script
    Saving the Hello World Bash script



  5. You should now be returned to your terminal as the nano text editor closes. The next thing we need to do is give execute permissions to the script we have just created. If we don’t, it will not be possible to execute the script. Enter the following command in your terminal.
    $ chmod +x hello-world.sh
    
  6. Finally, we can now execute the Hello World Bash script. Since the script is in our present working directory, we can run the script by simply typing ./ and the file name. The command below will execute your new Bash script.
    $ ./hello-world.sh
    
    The results of executing our Hello World Bash script on Linux
    The results of executing our Hello World Bash script on Linux

Congratulations! You have just taken your first step in Bash scripting. The steps above have shown you how to create new files, declare the Bash shell as an interpreter, use the echo command to output text, how to give execute permissions to a script, and finally how to execute a script in terminal.

Alternative Hello World Script

In Bash, there are almost always numerous ways to do something. We can make our Hello World script a little more complicated by using the following instead:

#!/bin/bash

# declare STRING variable
STRING="Hello World"

# print variable on a screen
echo $STRING

This will have the same exact effect as the first script we went over above, but shows you how to declare a variable as well. Note that the lines preceded by pound signs # are just comments and will not be executed as part of the script.

Closing Thoughts




In this tutorial, you learned how to create and execute your first Hello World Bash script on Linux. Since we assume this is one of your first interactions with the command line terminal, we also showed the steps to create the file, give it execute permissions, etc. Just in case you wanted a bit more of a challenge, we also included the alternative Hello World script. Check out our other Bash script tutorials for next steps.



Comments and Discussions
Linux Forum