Bird
0
0

Identify the error in this script snippet:

medium📝 Debug Q14 of 15
Bash Scripting - Variables
Identify the error in this script snippet:
export VAR=hello
function test() {
  VAR=world
  local VAR
  echo $VAR
}
test
echo $VAR
Alocal must be declared before assigning VAR inside function
Bexport cannot be used outside functions
Cecho $VAR inside function will print empty
DFunction syntax is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Understand local variable declaration order

    In Bash, local VAR must appear before assigning a value to VAR inside the function to make it local.
  2. Step 2: Analyze the script behavior

    Here, VAR=world assigns to global VAR before local VAR is declared, so local VAR is empty and echo $VAR prints empty.
  3. Final Answer:

    local must be declared before assigning VAR inside function -> Option A
  4. Quick Check:

    Declare local before assignment inside functions [OK]
Quick Trick: Declare local variables before assigning values inside functions [OK]
Common Mistakes:
MISTAKES
  • Assigning before declaring local causes unexpected behavior
  • Thinking export is invalid outside functions
  • Assuming function syntax is wrong

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes