📖 Scenario: You are working as a junior system administrator. Your team wants all bash scripts to follow a clear style guide. This helps everyone read and maintain scripts easily.
🎯 Goal: You will create a simple bash script following good style and naming conventions. This includes using clear variable names, comments, indentation, and consistent formatting.
📋 What You'll Learn
Create variables with lowercase names and underscores
Add comments explaining the script
Use indentation for readability
Use double quotes around variables
Print a clear message as output
💡 Why This Matters
🌍 Real World
Following style guides in bash scripting helps teams maintain scripts that automate system tasks, backups, and monitoring.
💼 Career
System administrators and DevOps engineers write bash scripts daily. Clear style and conventions reduce errors and improve collaboration.
Progress0 / 4 steps
1
Create variables with clear names
Create two variables called user_name and user_age. Set user_name to "Alice" and user_age to 30.
Bash Scripting
Hint
Use lowercase letters and underscores for variable names. Assign strings with quotes and numbers without quotes.
2
Add comments and use indentation
Add a comment above the variables explaining they store user info. Then add a comment below explaining the script prints a greeting. Use indentation for the print command in the next step.
Bash Scripting
Hint
Start comments with # and keep them short and clear. Leave a blank line between sections for readability.
3
Use double quotes around variables in echo
Write an echo command that prints: Hello, Alice! You are 30 years old. Use the variables user_name and user_age inside double quotes. Indent the echo command with 4 spaces.
Bash Scripting
Hint
Use double quotes around the whole string and $variable to insert values. Indent with spaces, not tabs.
4
Run the script and check output
Run the script and print the output. The output should exactly be: Hello, Alice! You are 30 years old.
Bash Scripting
Hint
Run the script in your terminal or use bash script.sh if saved in a file.
Practice
(1/5)
1. Which of the following is the best practice for naming variables in a bash script according to style guides?
easy
A. Use clear, lowercase names with underscores, like file_count
B. Use uppercase names with spaces, like FILE COUNT
C. Use mixed case without underscores, like FileCount
D. Use numbers only, like 123
Solution
Step 1: Understand variable naming conventions
Bash style guides recommend clear, lowercase variable names with underscores for readability.
Step 2: Compare options
Use clear, lowercase names with underscores, like file_count uses lowercase and underscores, which is clear and readable. Other options use spaces, uppercase, or numbers only, which are not recommended.
Final Answer:
Use clear, lowercase names with underscores, like file_count -> Option A
Quick Check:
Variable naming = lowercase_with_underscores [OK]
Hint: Pick lowercase with underscores for variable names [OK]
Common Mistakes:
Using uppercase or spaces in variable names
Using camelCase instead of underscores
Using numbers only as variable names
2. Which of the following is the correct way to add a comment in a bash script?
easy
A. # This is a comment
B. /* This is a comment */
C. // This is a comment
D.
Solution
Step 1: Identify bash comment syntax
Bash uses the # symbol to start a comment line.
Step 2: Check each option
# This is a comment uses #, which is correct. Options B, C, and D use comment styles from other languages.
Final Answer:
# This is a comment -> Option A
Quick Check:
Comment start = # [OK]
Hint: Use # to start comments in bash scripts [OK]
Common Mistakes:
Using /* */ like in C
Using // like in JavaScript
Using like in HTML
3. What will be the output of the following bash script snippet?
count=3
if [ $count -gt 0 ]; then
echo "Count is positive"
fi
medium
A. No output
B. Syntax error
C. Count is positive
D. Count is zero
Solution
Step 1: Evaluate the condition
The variable count is 3, which is greater than 0, so the condition [ $count -gt 0 ] is true.
Step 2: Determine the output
Since the condition is true, the script executes echo "Count is positive", printing that line.
Final Answer:
Count is positive -> Option C
Quick Check:
3 > 0 prints message [OK]
Hint: If condition true, echo runs; else no output [OK]
Common Mistakes:
Confusing syntax causing errors
Assuming no output if condition true
Misreading the comparison operator
4. Identify the error in this bash script snippet:
for i in 1 2 3
do
echo $i
done
medium
A. Missing semicolon after 3
B. Incorrect indentation of echo
C. Missing do keyword
D. No error, script runs correctly
Solution
Step 1: Check for syntax correctness
The for loop syntax is correct: for i in 1 2 3; do ... done. The newline before do is allowed.
Step 2: Verify indentation and keywords
Indentation is recommended but not required. The do keyword is present. No semicolon needed before newline.
Final Answer:
No error, script runs correctly -> Option D
Quick Check:
Newline before do is valid [OK]
Hint: Newline before do is allowed; indentation is style only [OK]
Common Mistakes:
Thinking semicolon is mandatory before newline
Confusing indentation with syntax error
Missing do keyword
5. You want to write a bash script that reads a filename from the user, checks if it exists, and prints a message. Which style guide practice should you follow to make your script clear and maintainable?
hard
A. Use uppercase variable names and no comments
B. Use clear lowercase variable names with underscores and add comments explaining steps
C. Write all code in one line without indentation
D. Avoid using comments and use single-letter variable names
Solution
Step 1: Recall style guide recommendations
Good style means using clear, lowercase variable names with underscores and adding comments to explain the script.
Step 2: Evaluate options
Use clear lowercase variable names with underscores and add comments explaining steps follows these practices. Other options use uppercase, no comments, poor formatting, or unclear names, which reduce clarity.
Final Answer:
Use clear lowercase variable names with underscores and add comments explaining steps -> Option B
Quick Check:
Clear names + comments = maintainable script [OK]
Hint: Use clear names and comments for readable scripts [OK]
Common Mistakes:
Skipping comments thinking code is self-explanatory
Using uppercase or single-letter variables
Writing code without indentation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.