0
0
Bash Scriptingscripting~10 mins

Recursive functions in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Recursive functions
Call function with n
Check base case: n == 0?
YesReturn base value
No
Call function with n-1
Receive result from smaller call
Combine result with current n
Return combined result
Back to previous call or end
The function calls itself with a smaller input until it reaches a base case, then returns values back up the call chain.
Execution Sample
Bash Scripting
#!/bin/bash
factorial() {
  if [ $1 -eq 0 ]; then
    echo 1
  else
    local prev=$(factorial $(( $1 - 1 )))
    echo $(( $1 * prev ))
  fi
}
factorial 3
Calculates factorial of 3 by calling itself with decreasing numbers until 0, then multiplies results back up.
Execution Table
StepFunction CallParameter nBase Case?ActionOutput
1factorial 33NoCall factorial 2
2factorial 22NoCall factorial 1
3factorial 11NoCall factorial 0
4factorial 00YesReturn 11
5factorial 11NoCalculate 1 * 11
6factorial 22NoCalculate 2 * 12
7factorial 33NoCalculate 3 * 26
8EndAll calls returned6
💡 Reached base case at n=0, then returned results up the call stack until original call completed.
Variable Tracker
VariableStartAfter Step 4After Step 5After Step 6After Step 7Final
n30 (in deepest call)1233
prev11266
Output11266
Key Moments - 3 Insights
Why does the function call itself with n-1 instead of n+1?
Because the function needs to reach the base case where n=0 by decreasing n each time, as shown in execution_table steps 1-4.
What happens when the base case is reached?
The function returns 1 immediately without further calls, as seen in step 4, which stops the recursion from continuing infinitely.
How does the function combine results from recursive calls?
Each call waits for the smaller call to return, then multiplies the returned value by its own n, shown in steps 5-7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output when factorial is called with n=1 (step 5)?
A0
B2
C1
D3
💡 Hint
Check the Output column at step 5 in the execution_table.
At which step does the base case occur in the execution table?
AStep 4
BStep 7
CStep 1
DStep 3
💡 Hint
Look for the row where Base Case? is Yes.
If the base case returned 0 instead of 1, what would be the final output for factorial 3?
A6
B0
C3
D1
💡 Hint
Consider how multiplication with 0 affects the final result in the variable_tracker.
Concept Snapshot
Recursive functions call themselves with smaller inputs.
They must have a base case to stop recursion.
Each call waits for the smaller call to return.
Results combine as calls return back up.
In bash, use local variables to store recursive results.
Always test base case to avoid infinite loops.
Full Transcript
This example shows a bash recursive function calculating factorial. The function calls itself with n-1 until it reaches 0, the base case, where it returns 1. Then each call multiplies its n by the returned value from the smaller call. The execution table traces each call and return step. Variables n and prev change as recursion deepens and unwinds. Key moments clarify why n decreases, what base case means, and how results combine. The quiz tests understanding of outputs and base case location. The snapshot summarizes recursion essentials in bash scripting.