A First Script in Bash: Hello World in Shell Scripting

Gudisa Gebi
5 min readMar 28, 2024

--

In programming tutorials, the very basic “Hello World” program is usually the starting point when learning a new language or technology. But in shell scripting, even a simple “Hello World” script can be trickier than it seems. In this guide, we’ll explore the ins and outs of shell scripting by creating our own version of “Hello World” and understanding its core concepts. This will give you a strong base to begin mastering shell scripting.

Understanding the Script:

Let’s start by dissecting our first shell script, creatively named first.sh. Here's a breakdown of its components:

Shebang Line:

#!/bin/sh

Shebang Line:

At the top of the script, you’ll see a line that starts with #!. This line is called the “shebang” line. Following the #!, it says /bin/sh. This line instructs the Unix operating system to interpret the script using the Bourne shell, which is a standard shell program found at the location /bin/sh on most Unix-like systems.

In simpler terms, this line ensures that when you run the script, it’s executed using the Bourne shell, providing a common starting point for the script to be understood and executed correctly by the operating system.

Comments:

# This is a comment!

Comments in shell scripts begin with a # symbol. They are ignored by the shell and are used for documentation purposes.

Echo Command:

echo "Hello World"

The echo command is used to print text to the standard output. Here, it prints the string "Hello World".

The Whole Code

#!/bin/sh

# This is a comment by Gudisa!
echo "Hello World"

This script simply prints "Hello World" to the terminal. It's a basic example of a shell script.

If you’ve written “Hello World” in other programming languages, this script might look familiar. It’s essentially doing the same thing: printing “Hello World” to the screen. However, it’s written specifically for shell scripting.

  1. The first line, #!/bin/sh, is the shebang line. It tells Unix to interpret the script using /bin/sh, which is the standard Bourne shell. This ensures the script is executed in a shell environment.
  2. The comments (lines starting with #) provide explanations about the code. They are there for documentation purposes and are ignored by the shell when the script runs.
  3. The echo "Hello World" line is where the magic happens. The echo command is used to print text to the standard output (usually the terminal). Here, it prints the string "Hello World".

Running the Script:

After we’ve made our script, first.sh, ready to run by granting it the necessary permissions with chmod 755 first.sh, we're all set to execute it.

To run the script, we simply type ./first.sh in the terminal. Here, the ./ part tells the terminal that we want to execute something from the current directory, and first.sh is the name of our script.

When we hit Enter, our script springs into action! It does its job, which is to display a friendly greeting. In this case, it prints out “Hello World” right there in the terminal for us to see. It’s a simple but crucial step in learning how to create and run shell scripts.

Exploring Variations:

1.Adding Spaces:

echo Hello      World

One might expect this command to output “Hello” followed by multiple spaces and then “World”. However, the reality is different due to how the shell treats whitespace.

Shell Behavior: The shell, by default, treats consecutive whitespace characters as a single space. This behavior applies not only to the echo command but to all commands and arguments passed to the shell.

2.Quoting:

echo "Hello      World"

By wrapping the string in double quotes, like "Hello World", we preserve the whitespace. The shell considers the entire quoted string as a single argument to the echo command.

Key Points:

  1. Whitespace Preservation: Double quotes preserve whitespace within strings, preventing the shell from interpreting consecutive spaces as a single space.
  2. Single Argument: When a string is enclosed in double quotes, the shell treats it as a single argument, regardless of internal whitespace or special characters.
  3. Practical Application: Understanding the power of quoting is crucial for various shell scripting tasks:
  • File Paths: Quoting is essential when dealing with file paths containing spaces or special characters to ensure they are interpreted correctly by commands.
  • Command Substitution: Quoting is used to preserve the output of command substitution ($(...) or `...`), especially when the output contains whitespace or special characters.

3.Wildcards:

echo "Hello * World"

In the script echo "Hello * World", the asterisk (*) serves as a wildcard character in the shell. When the script runs, the shell interprets the asterisk as a placeholder for filenames in the current directory.

For example, if there are files named file1.txt, file2.txt, and file3.txt in the directory where the script is executed, the output will be:

Hello file1.txt file2.txt file3.txt World

Here, the asterisk is replaced by the names of the files in the directory. However, if there are no files in the directory, the asterisk remains unchanged in the output, treated simply as a literal character:

Hello * World

This demonstrates the dynamic behavior of wildcards in shell scripting, where they expand to match existing filenames, but if there are no matches, they are treated as regular characters.

Conclusion

In conclusion, our journey through the creation and exploration of a “Hello World” script in shell scripting has provided valuable insights into the fundamentals of shell scripting. We’ve dissected the script, understanding its components such as the shebang line, comments, and the echo command. By running the script and exploring variations, we’ve learned how the shell interprets whitespace, the significance of quoting, and the dynamic behavior of wildcards.

Through this process, we’ve gained a solid foundation for understanding shell scripting concepts and techniques. From basic printing to more advanced scripting tasks involving file paths and command substitution, the skills learned here are essential for mastering shell scripting.

Whether you’re just starting your journey into shell scripting or looking to deepen your understanding, remember the key points highlighted throughout this guide. With practice and exploration, you’ll continue to unlock the full potential of shell scripting for various tasks and projects. Happy scripting!

Day1 Blog

--

--