Recursion is an interesting and sometimes useful form of self-reference. Herbert Mayer defines it as ". . . expressing an algorithm by using a simpler version of that same algorithm . . ."

Consider a definition defined in terms of itself, an expression implicit in its own expression, a snake swallowing its own tail, or . . . a function that calls itself.

Example 24-13. Demonstration of a simple recursive function #!/bin/bash # recursion-demo.sh # Demonstration of recursion. RECURSIONS=9 # How many times to recurse. r_count=0 # Must be global. Why? recurse () { var="$1" while [ "$var" -ge 0 ] do echo "Recursion count = "$r_count" +-+ \$var = "$var"" (( var-- )); (( r_count++ )) recurse "$var" # Function calls itself (recurses) done #+ until what condition is met? } recurse $RECURSIONS exit $?