Bird
0
0

What will be the output of the following script? ```bash MYVAR=hello function test_func() { local MYVAR=world echo "$MYVAR" } test_func echo "$MYVAR" ```

medium📝 Predict Output Q4 of 15
Bash Scripting - Variables
What will be the output of the following script? ```bash MYVAR=hello function test_func() { local MYVAR=world echo "$MYVAR" } test_func echo "$MYVAR" ```
Ahello hello
Bhello world
Cworld world
Dworld hello
Step-by-Step Solution
Solution:
  1. Step 1: Analyze variable inside function

    Inside 'test_func', 'local MYVAR=world' creates a local variable 'MYVAR' with value 'world'. The echo prints 'world'.
  2. Step 2: Analyze variable outside function

    Outside the function, 'MYVAR' remains 'hello' because the local variable inside the function does not affect it. The echo prints 'hello'.
  3. Final Answer:

    world hello -> Option D
  4. Quick Check:

    Local variables inside functions do not change global variables [OK]
Quick Trick: Local inside function hides global variable temporarily [OK]
Common Mistakes:
MISTAKES
  • Assuming local variable changes global variable
  • Confusing output order
  • Ignoring local keyword effect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes