
Hello! Welcome to Day 8: The Bash Scripting Challenge. Today, we'll cover a few important Bash basics like comments, echo, variables, and wildcards. If you're new to scripting or just want to solidify your understanding, this guide is for you.
The #!/bin/bash at the beginning of a script is called a shebang (or hashbang). It tells the system which interpreter should be used to execute the script. In this case, it's specifying the Bash shell.
#!/bin/bash
Task 1: Comments - Keeping Your Code Clear
Comments are notes you leave in your code to explain what each section does. They’re especially useful when you come back to your script later or when others read your code.
In Bash, you can add a comment by starting the line with a #. For longer explanations, you can also add multiline comments:
# Task 1: Comments and Multiline Comments
# This is how you make a single line comment
<< COMMENT
This is a multiline comment explaining the structure of the script.
It covers comments, echo, variables, built-in variables, and wildcards.
COMMENT
Task 2: Echo - Displaying Messages
echo is a simple way to print messages to the terminal. It’s often used to give feedback or display output during script execution. Let’s take an example:
# Task 2: Echo
echo "Its Day 8. Keep practicing and work hard"
This makes your script more interactive and gives it a friendly feel.
Task 3: Variables - Storing Data
Variables are a way to store data that you want to use later in your script. You can store numbers, strings, filenames, or even the result of commands.
Let’s create two variables to hold numbers:
# Task 3: Variables - Declare two numbers
number1=42
number2=58
You can replace the numbers with anything you want, variables are flexible and useful for all sorts of tasks.
Task 4: Using Variables for Basic Arithmetic
Now that we have variables, let's do something simple with them. Here's how to add two numbers stored in variables:
sum=$((number1 + number2))
echo "The sum of $number1 and $number2 is: $sum"
In this example, the script adds number1 and number2 and prints the result. Bash makes it easy to handle simple calculations like this.
Task 5: Using Built-in Variables - Accessing System Info
Bash provides several built-in variables that give you immediate access to useful information about the script and the environment it’s running in. These variables are predefined and can be used to make your script more dynamic and responsive. Let's break down the ones we used:
$0: This variable holds the name of the script being executed. It’s useful when you want to display the script name in your output, especially if you're running multiple scripts.$#: This variable tells you how many arguments were passed to the script. This is helpful if your script requires inputs or parameters, and you want to ensure the correct number of arguments has been provided.$USER: This variable shows the username of the person currently running the script. It’s handy for scripts that interact with specific users or perform tasks based on the user’s identity.$RANDOM: This variable generates a random number every time it’s called. It's great for adding a bit of unpredictability to your script, whether you’re picking random values or making your script more interactive.
# Task 5: Built-in Variables - Display script name, elapsed time, and a random number
echo "You are currently running: $0"
echo "The number of arguments passed: $#"
echo "The current user is: $USER"
echo "Random number of the moment: $RANDOM. Feeling lucky? "
Task 6: Wildcards - Matching File Patterns
Wildcards are characters that allow you to match multiple files with similar names or patterns. They are useful when you’re working with lots of files and don’t want to type out each one. Let’s look at some examples:
To list all .txt or .log files in your directory and files starting with a vowel
# Task 6: Wildcards - List .txt and .log files, and files starting with a vowel
echo "Listing all the .txt and .log files in this directory:"
ls *.{txt,log}
echo "Files starting with a vowel:"
ls [aeiouAEIOU]*
These wildcard tricks save time and make your script more powerful when managing files.
The Complete Bash Script
Below is the complete script that covers all the tasks from today:
#!/bin/bash
# Day 8 Bash Scripting Challenge
# Task 1: Comments and Multiline Comments
# This is how you make a single line comment
<< COMMENT
This is a multiline comment explaining the structure of the script.
It covers comments, echo, variables, built-in variables, and wildcards.
COMMENT
echo ""
# Task 2: Echo
echo "Its Day 8. Keep practicing and work hard"
# Task 3: Variables - Declare two numbers
number1=42
number2=58
echo ""
# Task 4: Using Variables - Add two numbers and print the result
sum=$((number1 + number2))
echo "The sum of $number1 and $number2 is: $sum"
echo ""
# Task 5: Built-in Variables - Display script name, number of arguments, current user and a random number
echo "You are currently running: $0"
echo "The number of arguments passed: $#"
echo "The current user is: $USER"
echo "Random number of the moment: $RANDOM. Feeling lucky? "
echo ""
# Task 6: Wildcards - List .txt and .log files, and files starting with a vowel
echo "Listing all the .txt and .log files in this directory:"
ls *.{txt,log}
echo "Files starting with a vowel:"
ls [aeiouAEIOU]*
The echo "" statements used between certain lines enhance readability by adding spaces.
Save the file with a name such as ‘day8_bash_script.sh’
Don’t forget to give executable permissions for the script to run.
chmod +x day8_bash_script.sh
Finally, you can run the script as follows:
bash day8_bash_script.sh arg1 arg2 arg3
OR
./day8_bash_script.sh arg1 arg2 arg3
The output would look something like this:
ubuntu@ip-172-31-23-226:~/devops$ bash day8_bash_script.sh arg1 arg2 arg3
Its Day 8. Keep practicing and work hard
The sum of 42 and 58 is: 100
You are currently running: day8_bash_script.sh
The number of arguments passed: 3
The current user is: ubuntu
Random number of the moment: 136. Feeling lucky?
Listing all the .txt and .log files in this directory:
demo.txt hello.txt imp.log
Files starting with a vowel:
abc.sh imp.log install_nginx.sh install_package.sh
Conclusion
Today, we covered key Bash scripting concepts that will enhance your skills. By mastering comments, variables, and wildcards, you're on your way to writing clearer and more efficient scripts. Keep exploring!




