Bird
0
0

What will be the output of this script?

medium📝 Command Output Q13 of 15
Bash Scripting - Variables
What will be the output of this script?
VAR=hello
function test_func() {
  local VAR=world
  echo $VAR
}
test_func
echo $VAR
Aworld hello
Bhello hello
Cworld world
Dhello world
Step-by-Step Solution
Solution:
  1. Step 1: Analyze variable inside function

    The function declares a local variable VAR with value 'world', so echo $VAR inside prints 'world'.
  2. Step 2: Analyze variable outside function

    The global VAR remains 'hello', so echo $VAR after function call prints 'hello'.
  3. Final Answer:

    world hello -> Option A
  4. Quick Check:

    Local inside function, global outside [OK]
Quick Trick: Local variables override global only inside functions [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