Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Style guide and conventions in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Bash Style Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output of this script regarding variable naming?
Consider this bash script snippet that follows common style conventions for variable names. What will be the output?
Bash Scripting
MY_VAR=5
my_var=10
echo "$MY_VAR $my_var"
A5 10
B10 5
CMY_VAR my_var
D5 5
Attempts:
2 left
💡 Hint
Variable names in bash are case-sensitive.
📝 Syntax
intermediate
1:30remaining
Which option correctly follows the style guide for function definitions in bash?
Select the function definition that follows common bash style conventions for readability and compatibility.
A
function myFunc() {
  echo "Hello"
}
B
function myFunc {
  echo "Hello"
}
C
myFunc ()
{
  echo "Hello"
}
D
myFunc() {
  echo "Hello"
}
Attempts:
2 left
💡 Hint
The preferred style avoids the 'function' keyword and places parentheses immediately after the function name.
🔧 Debug
advanced
2:00remaining
Identify the style violation causing the script to fail in strict mode
This script fails when run with 'set -u' (treat unset variables as errors). What style issue causes this failure?
Bash Scripting
echo "User: $USER_NAME"
if [ "$USER_NAME" = "admin" ]; then
  echo "Welcome admin"
fi
AUSER_NAME is used without being initialized, violating style to always initialize variables.
BThe if statement uses single brackets instead of double brackets.
CThe echo command lacks quotes around the variable.
DThe script uses uppercase variable names which is discouraged.
Attempts:
2 left
💡 Hint
Strict mode requires variables to be initialized before use.
🧠 Conceptual
advanced
1:30remaining
Why is it recommended to use lowercase variable names for local variables in bash scripts?
Choose the best explanation for why local variables in bash scripts are often named in lowercase.
ALowercase variables are easier to type on the keyboard.
BLowercase variables avoid conflicts with environment variables which are usually uppercase.
CLowercase variables are automatically exported to child processes.
DLowercase variables run faster in bash scripts.
Attempts:
2 left
💡 Hint
Think about environment variables and naming conventions.
🚀 Application
expert
2:00remaining
What is the output of this script following style conventions for quoting and command substitution?
Analyze the script and select the correct output. The script uses recommended style for quoting and command substitution.
Bash Scripting
files_count=$(ls -1 | wc -l)
echo "Number of files: $files_count"
ANumber of files: ls -1 | wc -l
BNumber of files: $(ls -1 | wc -l)
CNumber of files: 3
DNumber of files:
Attempts:
2 left
💡 Hint
Command substitution captures the output of the command inside $(...).

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

  1. Step 1: Understand variable naming conventions

    Bash style guides recommend clear, lowercase variable names with underscores for readability.
  2. 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.
  3. Final Answer:

    Use clear, lowercase names with underscores, like file_count -> Option A
  4. 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

  1. Step 1: Identify bash comment syntax

    Bash uses the # symbol to start a comment line.
  2. Step 2: Check each option

    # This is a comment uses #, which is correct. Options B, C, and D use comment styles from other languages.
  3. Final Answer:

    # This is a comment -> Option A
  4. 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

  1. Step 1: Evaluate the condition

    The variable count is 3, which is greater than 0, so the condition [ $count -gt 0 ] is true.
  2. Step 2: Determine the output

    Since the condition is true, the script executes echo "Count is positive", printing that line.
  3. Final Answer:

    Count is positive -> Option C
  4. 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

  1. 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.
  2. Step 2: Verify indentation and keywords

    Indentation is recommended but not required. The do keyword is present. No semicolon needed before newline.
  3. Final Answer:

    No error, script runs correctly -> Option D
  4. 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

  1. Step 1: Recall style guide recommendations

    Good style means using clear, lowercase variable names with underscores and adding comments to explain the script.
  2. 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.
  3. Final Answer:

    Use clear lowercase variable names with underscores and add comments explaining steps -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard