Bird
0
0

Why does exporting a variable inside a Bash function not always make it available outside the function?

hard🧠 Conceptual Q10 of 15
Bash Scripting - Variables
Why does exporting a variable inside a Bash function not always make it available outside the function?
ABecause local variables inside functions are destroyed after function ends, even if exported.
BBecause export only works for global variables declared before the function.
CBecause Bash does not support exporting variables inside functions.
DBecause exported variables inside functions are only available to parent processes.
Step-by-Step Solution
Solution:
  1. Step 1: Understand local variable lifetime

    Local variables inside functions exist only during function execution and are destroyed afterward.
  2. Step 2: Understand export effect on local variables

    Exporting a local variable exports it only in the function's shell environment, which ends after function returns, so it is not available outside.
  3. Final Answer:

    Because local variables inside functions are destroyed after function ends, even if exported. -> Option A
  4. Quick Check:

    Local variable lifetime limits export effect outside function [OK]
Quick Trick: Local vars vanish after function, export can't extend lifetime [OK]
Common Mistakes:
MISTAKES
  • Thinking export inside function makes variable global
  • Believing export only works for pre-declared globals
  • Confusing parent and child process variable scope

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes