Bird
0
0

Given this script: ```bash export GLOBAL_VAR=100 function modify_vars() { local LOCAL_VAR=200 GLOBAL_VAR=300 } modify_vars echo "$GLOBAL_VAR $LOCAL_VAR" ``` What will be the output and why?

hard🚀 Application Q9 of 15
Bash Scripting - Variables
Given this script: ```bash export GLOBAL_VAR=100 function modify_vars() { local LOCAL_VAR=200 GLOBAL_VAR=300 } modify_vars echo "$GLOBAL_VAR $LOCAL_VAR" ``` What will be the output and why?
A300 200, both changed globally
B100 200, because GLOBAL_VAR unchanged, LOCAL_VAR local inside function
C300 (space) empty, because GLOBAL_VAR changed globally, LOCAL_VAR is local
D100 empty, because GLOBAL_VAR unchanged, LOCAL_VAR local
Step-by-Step Solution
Solution:
  1. Step 1: Analyze GLOBAL_VAR change

    GLOBAL_VAR is exported and reassigned inside function without 'local', so it changes globally to 300.
  2. Step 2: Analyze LOCAL_VAR scope

    LOCAL_VAR is declared local inside function, so it does not exist outside; echo prints empty for it.
  3. Final Answer:

    300 (space) empty, because GLOBAL_VAR changed globally, LOCAL_VAR is local -> Option C
  4. Quick Check:

    Global vars change globally; local vars stay inside functions [OK]
Quick Trick: Local vars vanish outside; globals persist and change [OK]
Common Mistakes:
MISTAKES
  • Expecting local variable to print outside function
  • Assuming GLOBAL_VAR unchanged
  • Confusing export effect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes