Hello World: Your First Bash Script

by Pete
Published: Updated: 3 minutes read

Embarking on a journey into the world of Bash scripting is an exciting endeavor. Bash scripts are like digital assistants, carrying out tasks on your behalf with precision and speed. In this article, we’ll take you through your first steps in Bash scripting, helping you craft a simple “Hello World” script and laying the foundation for your scripting adventure.

Discovering the Magic of Bash

Before diving into the details, let’s understand what Bash is. Bash stands for “Bourne Again Shell,” and it’s a powerful command-line interface that allows you to interact with your computer through text-based commands. Bash scripts extend this interaction, enabling you to automate sequences of commands and actions.

Understanding Fundamental Syntax

Every scripting language has its syntax, and Bash is no exception. The syntax acts as the grammar that governs how you write your scripts. Let’s start with a few key elements:

  • Shebang: The #!/bin/bash line at the beginning of your script specifies the interpreter to use (in this case, Bash).
  • Comments: Use # to add comments that provide context and explanations within your script.

Crafting Your First Script – “Hello World”

Let’s dive in and create a “Hello World” script. Open your favorite text editor and type in the following lines:

# This is a simple Bash script that prints "Hello World!"
echo "Hello World!"

Save the file with a .sh extension (e.g., hello.sh). The .sh extension signifies that the file contains a Bash script.

Executing Your Script

hello world!

To run your script, you need to make it executable. Open your terminal and navigate to the directory where your script is located. Use the chmod command to grant execute permissions:

chmod +x hello.sh 

Now, you can run your script:


Voilà! You’ve just executed your first Bash script, and it greeted you with “Hello World!”

Grasping the Essentials

While the “Hello World” script is simple, it lays the foundation for more complex scripting endeavors. As you become more familiar with Bash, you’ll discover how to work with variables, create conditional statements, and use loops to automate intricate tasks.

Conclusion: Your Scripting Journey Begins

Congratulations on creating and executing your first Bash script! This is just the beginning of your scripting journey. Armed with fundamental syntax knowledge, you’re ready to explore more advanced concepts and create scripts that can perform a wide range of tasks. So, continue to experiment, learn, and embrace the magic of Bash scripting.