0
0
Bash Scriptingscripting~20 mins

Why functions organize reusable code in Bash Scripting - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bash Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why use functions in Bash scripts?
Which of the following best explains why functions help organize reusable code in Bash scripting?
AFunctions prevent any errors from happening in the script.
BFunctions automatically speed up the script execution by running code in parallel.
CFunctions allow you to write code once and run it multiple times without rewriting it.
DFunctions make the script run only once and then stop.
Attempts:
2 left
💡 Hint
Think about how repeating the same steps in a script can be simplified.
💻 Command Output
intermediate
1:30remaining
Output of a simple Bash function call
What is the output of this Bash script?
Bash Scripting
greet() {
  echo "Hello, $1!"
}
greet Alice
AError: command not found
Bgreet Alice
CHello, $1!
DHello, Alice!
Attempts:
2 left
💡 Hint
Look at how the function uses $1 to get the first argument.
🔧 Debug
advanced
2: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 Bob
AHello,
Bsay_hello: command not found
CHello, Bob
DSyntax error near unexpected token
Attempts:
2 left
💡 Hint
Check how the function uses variables and arguments.
🚀 Application
advanced
2: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?
A
process() {
  echo "Starting process"
  date
  echo "Process complete"
}
process
B
process {
  echo "Starting process"
  date
  echo "Process complete"
}
process()
C
function process {
  echo "Starting process"
  date
  echo "Process complete"
}
process
D
process() {
  echo "Starting process"
  date
  echo "Process complete"
}
process()
Attempts:
2 left
💡 Hint
Remember the correct syntax for defining and calling functions in Bash.
🧠 Conceptual
expert
2:30remaining
Why functions improve script maintenance
Which statement best explains how functions improve maintenance of Bash scripts?
AFunctions automatically fix bugs in the script when called.
BFunctions isolate code blocks so changes can be made in one place without affecting the whole script.
CFunctions make the script run faster by skipping unused code.
DFunctions prevent the script from running if there is an error.
Attempts:
2 left
💡 Hint
Think about how changing repeated code in many places can be hard.