Bird
0
0

In a Bash function, why would you use the local keyword when declaring a variable?

easy🧠 Conceptual Q1 of 15
Bash Scripting - Functions
In a Bash function, why would you use the local keyword when declaring a variable?
ATo restrict the variable's scope to the function only
BTo make the variable accessible globally
CTo declare a variable that persists after the function ends
DTo export the variable to child processes
Step-by-Step Solution
Solution:
  1. Step 1: Understand variable scope

    Variables declared without local inside functions are global by default.
  2. Step 2: Purpose of local

    The local keyword limits the variable's scope to the function, preventing side effects outside.
  3. Final Answer:

    To restrict the variable's scope to the function only -> Option A
  4. Quick Check:

    Local variables are function-scoped [OK]
Quick Trick: Local limits variable scope to function only [OK]
Common Mistakes:
MISTAKES
  • Assuming local variables are global
  • Using local to export variables
  • Thinking local variables persist after function ends

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes