0
0
Bash Scriptingscripting~20 mins

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

Choose your learning style9 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 $(...).