Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Recursive functions in Bash Scripting - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a recursive function that prints numbers from n down to 1.

Bash Scripting
print_numbers() {
  if [ $1 -le 0 ]; then
    return
  fi
  echo $1
  print_numbers [1]
}

print_numbers 5
Drag options to blanks, or click blank then click option'
A$(( $1 + 1 ))
B$1 + 1
C$(( $1 - 1 ))
D$1 - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using $1 + 1 which increases the number and causes infinite recursion.
Not using arithmetic expansion, causing syntax errors.
2fill in blank
medium

Complete the code to calculate the factorial of a number recursively.

Bash Scripting
factorial() {
  if [ $1 -le 1 ]; then
    echo 1
  else
    local prev=$(factorial [1])
    echo $(( $1 * prev ))
  fi
}

factorial 5
Drag options to blanks, or click blank then click option'
A$1 + 1
B$1 - 1
C$(( $1 + 1 ))
D$(( $1 - 1 ))
Attempts:
3 left
💡 Hint
Common Mistakes
Using $1 - 1 without arithmetic expansion causes errors.
Increasing the argument instead of decreasing it.
3fill in blank
hard

Fix the error in the recursive function that sums numbers from 1 to n.

Bash Scripting
sum_to_n() {
  if [ $1 -eq 1 ]; then
    echo 1
  else
    local prev=$(sum_to_n [1])
    echo $(( $1 + prev ))
  fi
}

sum_to_n 5
Drag options to blanks, or click blank then click option'
A$(( $1 - 1 ))
B$(( $1 + 1 ))
C$1 + 1
D$1 - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Increasing the argument causes infinite recursion.
Not using arithmetic expansion leads to syntax errors.
4fill in blank
hard

Fill both blanks to create a recursive function that computes the nth Fibonacci number.

Bash Scripting
fib() {
  if [ $1 -le 1 ]; then
    echo $1
  else
    local a=$(fib [1])
    local b=$(fib [2])
    echo $(( a + b ))
  fi
}

fib 6
Drag options to blanks, or click blank then click option'
A$(( $1 - 1 ))
B$1 - 1
C$(( $1 - 2 ))
D$1 - 2
Attempts:
3 left
💡 Hint
Common Mistakes
Using $1 - 1 without $(( )) causes errors.
Mixing arithmetic and string subtraction.
5fill in blank
hard

Fill all three blanks to create a recursive function that reverses a string.

Bash Scripting
reverse() {
  if [ -z "$1" ]; then
    echo ""
  else
    local first=${1:0:1}
    local rest=${1:1}
    local rev_rest=$(reverse [1])
    echo "${rev_rest}[2]${first}[3]"
  fi
}

reverse "hello"
Drag options to blanks, or click blank then click option'
A"$rest"
B"$first"
C""
D" "
Attempts:
3 left
💡 Hint
Common Mistakes
Using the first character instead of the rest in recursive call.
Adding spaces between characters when reversing.

Practice

(1/5)
1. What is the main purpose of a base case in a recursive Bash function?
easy
A. To increase the input value for the next call
B. To call the function again with the same input
C. To print the current value of a variable
D. To stop the recursion and prevent infinite loops

Solution

  1. Step 1: Understand recursion basics

    A recursive function calls itself repeatedly with smaller inputs.
  2. Step 2: Role of base case

    The base case stops the recursion to avoid infinite loops and eventual crash.
  3. Final Answer:

    To stop the recursion and prevent infinite loops -> Option D
  4. Quick Check:

    Base case = stop recursion [OK]
Hint: Base case always stops recursion to avoid endless calls [OK]
Common Mistakes:
  • Forgetting the base case causes infinite recursion
  • Using base case to increase input instead of stopping
  • Confusing base case with recursive step
2. Which of the following is the correct syntax to define a recursive function named countdown in Bash?
easy
A. countdown() { while [ $1 -gt 0 ]; do echo $1; (( $1-- )); done; echo Done; }
B. def countdown(n): if n <= 0: print('Done') else: print(n) countdown(n-1)
C. function countdown() { if [[ $1 -le 0 ]]; then echo Done; else echo $1; countdown $(( $1 - 1 )); fi }
D. function countdown { echo $1; countdown $(( $1 - 1 )); }

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]
Hint: Look for Bash syntax with base case and recursive call [OK]
Common Mistakes:
  • Using Python syntax in Bash function
  • Missing base case causing infinite recursion
  • Incorrect function declaration without parentheses
3. What will be the output of this Bash script?
factorial() {
  if [[ $1 -le 1 ]]; then
    echo 1
  else
    local prev=$(factorial $(( $1 - 1 )))
    echo $(( $1 * prev ))
  fi
}
factorial 3
medium
A. 1
B. 6
C. 3
D. 9

Solution

  1. Step 1: Understand factorial recursion

    factorial(3) calls factorial(2), which calls factorial(1). factorial(1) returns 1.
  2. Step 2: Calculate factorial values

    factorial(2) = 2 * 1 = 2; factorial(3) = 3 * 2 = 6.
  3. Final Answer:

    6 -> Option B
  4. Quick Check:

    3! = 6 [OK]
Hint: Factorial n = n * factorial(n-1), factorial(1) = 1 [OK]
Common Mistakes:
  • Confusing multiplication order
  • Forgetting base case returns 1
  • Assuming output is input value
4. Identify the error in this recursive Bash function that calculates the sum of numbers from 1 to n:
sum() {
  if [[ $1 -eq 0 ]]; then
    echo 0
  else
    echo $(( $1 + sum $(( $1 - 1 )) ))
  fi
}
sum 3
medium
A. Using echo inside arithmetic expression causes error
B. Missing base case for $1 == 1
C. Function name 'sum' is a reserved keyword
D. Incorrect decrement syntax $(( $1 - 1 ))

Solution

  1. Step 1: Analyze recursive call inside arithmetic

    The function calls sum recursively inside $(( ... )), but sum outputs text via echo, not a number directly.
  2. Step 2: Understand Bash arithmetic limitations

    Bash arithmetic expects numbers, but echo outputs string with newline, causing error.
  3. Final Answer:

    Using echo inside arithmetic expression causes error -> Option A
  4. Quick Check:

    Echo inside $(( )) = error [OK]
Hint: Avoid echo inside $(( )); use command substitution instead [OK]
Common Mistakes:
  • Assuming echo returns a number usable in arithmetic
  • Ignoring base case correctness
  • Thinking function name 'sum' is reserved
5. You want to write a recursive Bash function list_files that prints all files in a directory and its subdirectories. Which approach correctly handles recursion and avoids infinite loops?
hard
A. Check if the argument is a directory; if yes, list files and call list_files on each subdirectory; else print the file name
B. Call list_files on the directory without checking type, then print the directory name
C. Use a loop to list files only in the current directory without recursion
D. Call list_files on all files and directories without base case

Solution

  1. Step 1: Understand recursive directory traversal

    To list files recursively, check if the argument is a directory; if so, process contents recursively.
  2. Step 2: Avoid infinite loops by checking file type

    Only recurse into directories; print file names directly to avoid infinite recursion.
  3. Final Answer:

    Check if the argument is a directory; if yes, list files and call list_files on each subdirectory; else print the file name -> Option A
  4. Quick Check:

    Directory check + recursive call = correct recursion [OK]
Hint: Always check if input is directory before recursive call [OK]
Common Mistakes:
  • Recursing without checking if input is directory
  • Missing base case causing infinite recursion
  • Not printing files when input is a file
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard