DEV Community

Cover image for BashBlaze Day 1: Mastering the Basics of Bash Scripting
Muhammad Hanzala Ali
Muhammad Hanzala Ali

Posted on

BashBlaze Day 1: Mastering the Basics of Bash Scripting

Introduction

Welcome to Day 1 of the BashBlaze - 7 Days of Bash Scripting Challenge! Whether you're new to scripting or just brushing up, this day is all about the fundamentals of bash scripting. We'll cover comments, echo commands, variables, built-in variables, and wildcards to get you started. Each task is designed to give you hands-on experience with essential bash scripting features.

Let’s dive in and go through each task with a solution so you can follow along and implement them yourself!

Task 1: Comments in Bash

Comments are essential for making your scripts readable and maintainable. They help you and others understand what the script is doing at a glance.

Solution:

# Task 1: Comments
<< comment
    This script demonstrates how to use comments in bash.
comment

Enter fullscreen mode Exit fullscreen mode

In this script, we used both single-line comments (using #) and multi-line comments (using a here-document << comment). Adding comments to your code is a great way to provide context and make your scripts easy to understand.

Task 2: Using Echo in Bash

The echo command is one of the simplest yet most powerful commands for outputting text in bash scripts. You can use it to print any message to the terminal.

Solution:

# Task 2: Echo
echo "Hello World!"

Enter fullscreen mode Exit fullscreen mode

This script prints "Hello World!" to the terminal, a classic first step in learning any programming language. The echo command can be used in combination with variables and complex expressions, which we will explore in later tasks.

Task 3: Declaring Variables

Variables allow us to store and reuse data throughout our scripts. Let’s declare a variable and print its value.

Solution:

# Task 3: Variables
text="Declare Variable"
echo "$text"

Enter fullscreen mode Exit fullscreen mode

In this example, we store the string "Declare Variable" in the variable text and print its value using echo. Remember to use $ when referencing the value of a variable in bash.

Task 4: Using Variables to Perform Arithmetic

Variables are not just for strings; you can also use them for numerical operations. Let's use variables to calculate the sum of two numbers.

Solution:

# Task 4: Using Variables
a=5
b=3
sum=$((a+b))
echo "The sum is $sum"

Enter fullscreen mode Exit fullscreen mode

Here, we declare two variables a and b with the values 5 and 3, respectively. We then use the $(( ... )) syntax to perform arithmetic and print the result.

Task 5: Working with Built-in Variables

Bash comes with several built-in variables that provide useful information about the environment. Let's explore some of them.

Solution:

# Task 5: Using Built-in Variables
echo "Exit status of the last command: $?"
echo "Process ID of the current script: $$"
echo "Current working directory: $PWD"
echo "Home directory: $HOME"

Enter fullscreen mode Exit fullscreen mode
  • $? returns the exit status of the last executed command.
  • $$ gives the process ID of the current script.
  • $PWD displays the current working directory.
  • $HOME shows the home directory path. These built-in variables are very useful in real-world scripting scenarios.

Task 6: Using Wildcards in Bash

Wildcards are used for pattern matching, making it easy to work with files and directories. Let’s use a wildcard to list all files with a specific extension.

Solution:

# Task 6: Wildcards
echo "List all .sh files in the current directory:"
ls *.sh

Enter fullscreen mode Exit fullscreen mode

The *.sh wildcard matches all files with the .sh extension in the current directory. Wildcards are extremely handy when working with multiple files, especially for tasks like file manipulation and searching.

Conclusion

Congratulations on completing Day 1 of the BashBlaze Challenge! Today we covered the basics of bash scripting, from comments and echo commands to variables, built-in variables, and wildcards. These foundational skills will set you up for success as we move on to more advanced topics in the coming days. Stay tuned for Day 2, where we'll dive deeper into conditional statements and loops!

Make sure to commit your script to GitHub and share your progress. Happy scripting!

My GitHub repository link: Click Here

Key Takeaways

  • *Comments * help in explaining your code.
  • The echo command is used to print messages.
  • Variables store data that can be reused.
  • Built-in Variables provide useful information like the process ID, current directory, and more.
  • Wildcards allow you to match file patterns efficiently. If you found this helpful, be sure to check back for Day 2 of the BashBlaze Challenge, where we’ll take things up a notch!

Top comments (0)