📖 Scenario: You are creating a simple Bash script to greet users and show the current date. To make your script easy to understand and maintain, you will add comments explaining each part.
🎯 Goal: Build a Bash script that prints a greeting and the current date, with clear comments documenting each step.
📋 What You'll Learn
Create a Bash script with variables and commands
Add comments explaining the purpose of variables and commands
Print output to the terminal
💡 Why This Matters
🌍 Real World
Writing clear comments in scripts helps others and your future self understand what the script does, making maintenance easier.
💼 Career
Clear documentation is a key skill for system administrators, DevOps engineers, and anyone writing automation scripts.
Progress0 / 4 steps
1
Create a Bash script with a greeting variable
Create a Bash script and define a variable called greeting with the value "Hello, welcome to the script!".
Bash Scripting
Hint
Use greeting="Hello, welcome to the script!" to create the variable.
2
Add a comment explaining the greeting variable
Add a comment above the greeting variable that says # This variable stores the welcome message.
Bash Scripting
Hint
Start the comment line with # followed by the explanation.
3
Add a command to print the greeting with a comment
Add a comment # Print the greeting message and then write the command echo "$greeting" to print the greeting.
Bash Scripting
Hint
Use echo "$greeting" to display the message.
4
Add a comment and command to show the current date
Add a comment # Show the current date and time and then write the command date to display the current date and time.
Bash Scripting
Hint
Use the date command to display the current date and time.
Practice
(1/5)
1. What is the main purpose of comments in a Bash script?
easy
A. To explain what the code does for better understanding
B. To make the script run faster
C. To change the output of the script
D. To store variables for later use
Solution
Step 1: Understand the role of comments
Comments are lines starting with # that explain the code but do not affect execution.
Step 2: Identify the correct purpose
Comments help humans understand the script, not the computer or output.
Final Answer:
To explain what the code does for better understanding -> Option A
Quick Check:
Comments explain code = D [OK]
Hint: Comments explain code, they don't affect running [OK]
Common Mistakes:
Thinking comments speed up scripts
Believing comments change output
Confusing comments with variables
2. Which of the following is the correct way to write a comment in a Bash script?
easy
A. // This is a comment
B. # This is a comment
C.
D. /* This is a comment */
Solution
Step 1: Recall Bash comment syntax
Bash comments start with the # symbol and continue to the end of the line.
Step 2: Match the correct syntax
Only # This is a comment uses # correctly; others are for other languages.
Final Answer:
# This is a comment -> Option B
Quick Check:
Bash comments start with # = B [OK]
Hint: Bash comments always start with # [OK]
Common Mistakes:
Using // like in JavaScript
Using /* */ like in C
Using HTML comment style
3. What will be the output of this script?
#!/bin/bash
# Print greeting message
echo "Hello, world!" # This prints the message
medium
A. This prints the message
B. # Print greeting message
C. Hello, world!
D. No output
Solution
Step 1: Identify executable lines
Lines starting with # are comments and ignored; only echo prints output.
Step 2: Determine output of echo command
echo "Hello, world!" prints Hello, world! exactly.
Final Answer:
Hello, world! -> Option C
Quick Check:
Comments ignored, echo prints text = C [OK]
Hint: Only echo prints; comments do not show in output [OK]
Common Mistakes:
Thinking comments print
Confusing inline comments as output
Expecting comment text to appear
4. Identify the error in this Bash script snippet:
#!/bin/bash
# This is a comment
#echo "Hello"
echo "World"
medium
A. The line '#echo "Hello"' is treated as a comment and not executed
B. The comment symbol # is missing
C. The echo command is misspelled
D. The script will print both Hello and World
Solution
Step 1: Analyze the commented line
The line '#echo "Hello"' starts with #, so it is a comment and ignored.
Step 2: Check script output
Only echo "World" runs, so only 'World' prints, not 'Hello'.
Final Answer:
The line '#echo "Hello"' is treated as a comment and not executed -> Option A
Quick Check:
Lines starting with # are comments = A [OK]
Hint: Lines starting with # are never executed [OK]
Common Mistakes:
Thinking commented lines run
Missing # on comment lines
Expecting both outputs
5. You want to add a comment explaining a complex command in your Bash script. Which is the best practice?
hard
A. Add the comment without # anywhere in the script
B. Place the comment on the same line after the command using #
C. Put the comment inside double quotes after the command
D. Write the comment on the line before the command starting with #
Solution
Step 1: Understand comment placement
Comments before commands clearly explain what the command does before it runs.
Step 2: Evaluate options
Comments inside quotes or without # are treated as code or cause errors; inline comments are okay but less clear for complex commands.
Final Answer:
Write the comment on the line before the command starting with # -> Option D
Quick Check:
Comments before commands improve clarity = A [OK]
Hint: Put comments before complex commands for clarity [OK]
Common Mistakes:
Putting comments inside quotes
Omitting # for comments
Adding comments after commands for complex lines
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.