Day 8 Task: Shell Scripting Challenge


Welcome to Day 8 of the Bash Scripting Challenge! Today, we'll cover essential concepts such as comments, echo, variables, built-in variables, and wildcards. By the end of this challenge, you'll have a single bash script that demonstrates all these concepts. Let's get started!


Task 1: Comments

In bash scripts, comments are used to add explanatory notes or disable certain lines of code. They start with a #.

#!/bin/bash
# This is a comment
# The following script will perform various tasks to demonstrate bash scripting basics

Task 2: Echo

The echo command is used to display messages on the terminal.

#!/bin/bash
# Task 2: Using echo to print a message
echo "Hello, welcome to the Bash Scripting Challenge!"

๐Ÿ“ธ Screenshot:

Echo Command

Task 3: Variables

Variables in bash are used to store data and can be referenced by their name.

#!/bin/bash
# Task 3: Declaring variables and assigning values
greeting="Hello"
name="Alice"

Task 4: Using Variables

Now that you have declared variables, let's use them to perform a simple task.

#!/bin/bash
# Task 4: Using variables to perform a simple task
num1=10
num2=20
sum=$((num1 + num2))
echo "The sum of $num1 and $num2 is: $sum"

๐Ÿ“ธ Screenshot:

Using Variables

Task 5: Using Built-in Variables

Bash provides several built-in variables that hold useful information.

#!/bin/bash
# Task 5: Using built-in variables to display information
echo "Script Name: $0"
echo "Number of Arguments: $#"
echo "All Arguments: $@"

๐Ÿ“ธ Screenshot:

Built-in Variables

Task 6: Wildcards

Wildcards are special characters used to perform pattern matching when working with files.

#!/bin/bash
# Task 6: Using wildcards to list files with a specific extension
echo "Listing all .txt files in the current directory:"
ls *.txt

๐Ÿ“ธ Screenshot:

Wildcards

Complete Script

Here's the complete script that incorporates all the tasks mentioned above. Save this as bash_scripting_challenge.sh.

#!/bin/bash
# Bash Scripting Challenge: Day 8

# Task 1: Comments
# This script demonstrates basic bash scripting concepts.

# Task 2: Echo
# Using echo to print a message
echo "Hello, welcome to the Bash Scripting Challenge!"

# Task 3: Variables
# Declaring variables and assigning values
greeting="Hello"
name="Alice"

# Task 4: Using Variables
# Using variables to perform a simple task
num1=10
num2=20
sum=$((num1 + num2))
echo "The sum of $num1 and $num2 is: $sum"

# Task 5: Using Built-in Variables
# Using built-in variables to display information
echo "Script Name: $0"
echo "Number of Arguments: $#"
echo "All Arguments: $@"

# Task 6: Wildcards
# Using wildcards to list files with a specific extension
echo "Listing all .txt files in the current directory:"
ls *.txt

๐Ÿ“ธ Screenshot:

0
Subscribe to my newsletter

Read articles from Himanshu Palhade directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Himanshu Palhade
Himanshu Palhade