Bird
0
0

Which of the following is the correct syntax to define a recursive function named countdown in Bash?

easy📝 Syntax Q12 of 15
Bash Scripting - Functions
Which of the following is the correct syntax to define a recursive function named countdown in Bash?
Acountdown() { while [ $1 -gt 0 ]; do echo $1; (( $1-- )); done; echo Done; }
Bdef countdown(n): if n <= 0: print('Done') else: print(n) countdown(n-1)
Cfunction countdown() { if [[ $1 -le 0 ]]; then echo Done; else echo $1; countdown $(( $1 - 1 )); fi }
Dfunction countdown { echo $1; countdown $(( $1 - 1 )); }
Step-by-Step Solution
Solution:
  1. Step 1: Identify Bash function syntax

    Bash functions use the syntax: function name() { commands; } or name() { commands; }.
  2. Step 2: Check recursive call and base case

    function countdown() { if [[ $1 -le 0 ]]; then echo Done; else echo $1; countdown $(( $1 - 1 )); fi } correctly defines a function with a base case (if $1 <= 0) and recursive call countdown $(( $1 - 1 )).
  3. Final Answer:

    function countdown() { if [[ $1 -le 0 ]]; then echo Done; else echo $1; countdown $(( $1 - 1 )); fi } -> Option C
  4. Quick Check:

    Bash recursion syntax = function countdown() { if [[ $1 -le 0 ]]; then echo Done; else echo $1; countdown $(( $1 - 1 )); fi } [OK]
Quick Trick: Look for Bash syntax with base case and recursive call [OK]
Common Mistakes:
MISTAKES
  • Using Python syntax in Bash function
  • Missing base case causing infinite recursion
  • Incorrect function declaration without parentheses

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes