Bird
0
0

Find the bug in this recursive function:

medium📝 Debug Q7 of 15
Bash Scripting - Functions
Find the bug in this recursive function:
countdown() {
  if [[ $1 -lt 0 ]]; then
    echo "Done"
  else
    echo $1
    countdown $1 - 1
  fi
}
countdown 3
AFunction name countdown is invalid
BIncorrect argument syntax in recursive call; should use arithmetic expansion
CMissing local keyword for variables
DBase case condition should be $1 -le 0
Step-by-Step Solution
Solution:
  1. Step 1: Check recursive call argument

    The call countdown $1 - 1 passes two arguments: current $1 and string '- 1', not the decremented value.
  2. Step 2: Correct argument passing

    It should be countdown $(( $1 - 1 )) to pass the decremented number as a single argument.
  3. Final Answer:

    Incorrect argument syntax in recursive call; should use arithmetic expansion -> Option B
  4. Quick Check:

    Use $(( )) to decrement argument in recursion [OK]
Quick Trick: Use $(( )) to calculate recursive arguments [OK]
Common Mistakes:
MISTAKES
  • Passing multiple arguments instead of one
  • Wrong base case condition
  • Assuming function name invalid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes