Challenge - 5 Problems
Bash Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:30remaining
Why use functions in Bash scripts?
Which of the following best explains why functions help organize reusable code in Bash scripting?
Attempts:
2 left
💡 Hint
Think about how repeating the same steps in a script can be simplified.
✗ Incorrect
Functions let you group commands together and call them whenever needed, so you don't have to write the same code again and again.
💻 Command Output
intermediate1:30remaining
Output of a simple Bash function call
What is the output of this Bash script?
Bash Scripting
greet() {
echo "Hello, $1!"
}
greet AliceAttempts:
2 left
💡 Hint
Look at how the function uses $1 to get the first argument.
✗ Incorrect
The function greet prints 'Hello,' followed by the first argument passed to it, which is 'Alice'.
🔧 Debug
advanced2:00remaining
Identify the error in this Bash function
What error will this script produce when run?
Bash Scripting
say_hello() {
echo "Hello, $name"
}
say_hello BobAttempts:
2 left
💡 Hint
Check how the function uses variables and arguments.
✗ Incorrect
The function uses $name which is not set. The argument 'Bob' is passed but not assigned to $name inside the function.
🚀 Application
advanced2:00remaining
Refactor repeated code into a function
You have this repeated code in a Bash script:
echo "Starting process"
date
echo "Process complete"
Which function definition and call correctly replaces this repeated code?
Attempts:
2 left
💡 Hint
Remember the correct syntax for defining and calling functions in Bash.
✗ Incorrect
Option A correctly defines a function with parentheses and calls it by name without parentheses.
🧠 Conceptual
expert2:30remaining
Why functions improve script maintenance
Which statement best explains how functions improve maintenance of Bash scripts?
Attempts:
2 left
💡 Hint
Think about how changing repeated code in many places can be hard.
✗ Incorrect
Functions group related commands so you only need to update code inside the function once, making maintenance easier and safer.