Bird
0
0

Find the error in this script:

medium📝 Debug Q7 of 15
Bash Scripting - Functions
Find the error in this script:
print_msg() {
  echo "Message: $msg"
}
print_msg
msg="Hello"
AVariable msg is not defined before function call
BVariable msg is not accessible inside function
CFunction call syntax is wrong
DFunction definition syntax is invalid
Step-by-Step Solution
Solution:
  1. Step 1: Check variable definition timing

    The variable msg is defined after the function call, so it is not available when the function executes.
  2. Step 2: Verify variable availability at call time

    Variable msg is not defined before calling print_msg, so $msg inside the function is unset.
  3. Step 3: Identify actual error

    Variable msg must be defined before the function call to be accessible.
  4. Final Answer:

    Variable msg is not defined before function call -> Option A
  5. Quick Check:

    Variables must be defined before function call to be accessible [OK]
Quick Trick: Define variables before calling functions that use them [OK]
Common Mistakes:
MISTAKES
  • Assuming variables inside functions are local
  • Calling functions with parentheses
  • Defining variables after function call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes