Larger scripts often move reusable functions into helper files. A small selector can decide which helper library the entry script loads.

Program

Play the script to choose the helper and see the source plan.

helper
source_helper_plan.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash

helper="math"
case "$helper" in
    math) helper_file="lib/math.sh"; function_name="add_numbers" ;;
    text) helper_file="lib/text.sh"; function_name="trim_line" ;;
esac
echo "source $helper_file -> $function_name"
#!/usr/bin/env bash

helper="text"
case "$helper" in
    math) helper_file="lib/math.sh"; function_name="add_numbers" ;;
    text) helper_file="lib/text.sh"; function_name="trim_line" ;;
esac
echo "source $helper_file -> $function_name"
  1. helper ← math

    3helper="math"4case "$helper" in
    values this stepmathhelper
  2. case "$helper" in

    3helper="math"4case "$helper" in5    math) helper_file="lib/math.sh"; function_name="add_numbers" ;;
    values this stepmathhelper
  3. helper_file ← lib/math.sh, function_name ← add_numbers

    4case "$helper" in5    math) helper_file="lib/math.sh"; function_name="add_numbers" ;;6    text) helper_file="lib/text.sh"; function_name="trim_line" ;;
    values this steplib/math.shhelper_fileadd_numbersfunction_name
  4. echo "source $helper_file -> $function_name"

    7esac8echo "source $helper_file -> $function_name"
    outputsource lib/math.sh -> add_numbers
    values this steplib/math.shhelper_fileadd_numbersfunction_name
  1. helper ← text

    3helper="text"4case "$helper" in
    values this steptexthelper
  2. case "$helper" in

    3helper="text"4case "$helper" in5    math) helper_file="lib/math.sh"; function_name="add_numbers" ;;
    values this steptexthelper
  3. helper_file ← lib/text.sh, function_name ← trim_line

    5    math) helper_file="lib/math.sh"; function_name="add_numbers" ;;6    text) helper_file="lib/text.sh"; function_name="trim_line" ;;7esac
    values this steplib/text.shhelper_filetrim_linefunction_name
  4. echo "source $helper_file -> $function_name"

    7esac8echo "source $helper_file -> $function_name"
    outputsource lib/text.sh -> trim_line
    values this steplib/text.shhelper_filetrim_linefunction_name
source `source` loads shell definitions into the current script.
helper file A helper file groups related reusable functions.
entry script The entry script chooses what helpers it needs before running the main flow.