34.6. Oddities

Can a script recursively call itself? Indeed.

Example 34-6. A script that recursively calls itself

#!/bin/bash
# recurse.sh

#  Can a script recursively call itself?
#  Yes, but this is of little or no practical use
#+ except perhaps as a "proof of concept".

RANGE=10
MAXVAL=9

i=$RANDOM
let "i %= $RANGE"  # Generate a random number between 0 and $MAXVAL.

if [ "$i" -lt "$MAXVAL" ]
then
  echo "i = $i"
  ./$0             #  Script recursively spawns a new instance of itself.
fi                 #  Each child script does the same, until
                   #+ a generated $i equals $MAXVAL.

#  Using a "while" loop instead of an "if/then" test causes problems.
#  Exercise for the reader: Explain why.

exit 0

Caution

Too many levels of recursion can exhaust the script's stack space, causing a segfault.