Larger Script Organization
Source Helper
Choose a Library
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.
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"
helper ← math
3helper="math"4case "$helper" invalues this stepmathhelpercase "$helper" in
3helper="math"4case "$helper" in5 math) helper_file="lib/math.sh"; function_name="add_numbers" ;;values this stepmathhelperhelper_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_nameecho "source $helper_file -> $function_name"
7esac8echo "source $helper_file -> $function_name"outputsource lib/math.sh -> add_numbersvalues this steplib/math.shhelper_fileadd_numbersfunction_name
helper ← text
3helper="text"4case "$helper" invalues this steptexthelpercase "$helper" in
3helper="text"4case "$helper" in5 math) helper_file="lib/math.sh"; function_name="add_numbers" ;;values this steptexthelperhelper_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" ;;7esacvalues this steplib/text.shhelper_filetrim_linefunction_nameecho "source $helper_file -> $function_name"
7esac8echo "source $helper_file -> $function_name"outputsource lib/text.sh -> trim_linevalues 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.