Source files and reusable modules
Place reusable definitions in a trusted file and load it using source ./helpers.bash or . ./helpers.bash. Sourcing executes shell code in the current shell, so functions, assignments and other state changes can persist. Executing bash ./helpers.bash instead starts a child interpreter. Use an explicit trusted path rather than relying on PATH or the working directory. When a file is sourced from different working directories, a common Bash pattern is to compute its directory from ${BASH_SOURCE[0]} with dirname, cd in command substitution, and pwd -P; check each command’s status. This locates the directory of the invoked file path in ordinary filesystem layouts, not necessarily the ultimate target of a symlink; paths containing trailing newline characters also need special handling. Library files should define functions without unexpected top-level side effects. export -f name can pass a trusted function to a compatible child Bash, but is not a portable module or authentication mechanism; child shell settings may prevent import. Do not source untrusted files. An explicit relative path still depends on the current working directory. In example 2, CDPATH= cd -- "$dir" prevents a user-configured CDPATH from redirecting the lookup or adding a line of output to the captured directory. The fixture creation and source operations are guarded; this is a trusted local module example, not a sandbox.
Practical examples
Run each example separately in a fresh Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code.
Source a trusted fixture
printf '%s\n' "report() { printf '%s\\n' ready; }" > helpers.bash &&bash ./helpers.bash &&if ! declare -F report >/dev/null; then printf '%s\n' absent; fi &&source ./helpers.bash &&reportExpected stdout:
absentreadyThe child definition is not retained; sourcing installs it in the current shell.
Resolve a sibling module
mkdir app &&printf '%s\n' "report() { printf '%s\\n' loaded; }" > app/helpers.bash &&cat > app/main.bash <<'SCRIPT' &&dir=$(dirname -- "${BASH_SOURCE[0]}") || exit 1base=$(CDPATH= cd -- "$dir" && pwd -P) || exit 1source "$base/helpers.bash" || exit 1reportSCRIPTbash app/main.bashExpected stdout:
loadedThe driver runs from the parent directory but sources its sibling. The fixed fixture has no unusual path or symlink.
Export a trusted function
hello() { printf '%s\n' child; }export -f hellobash --noprofile --norc -c 'hello'export -n -f hellobash --noprofile --norc -c 'if ! declare -F hello >/dev/null; then printf "%s\n" absent; fi'Expected stdout:
childabsentRemoving the export attribute affects later children, not the current definition.
Common mistakes
Sourcing is code execution, not configuration parsing. Avoid sourcing downloaded scripts or assuming that the working directory is the library directory. Do not export arbitrary functions into unrelated processes.
Practice
In a new practice directory create a helpers.bash file defining a report function. Compare sourcing it with running it as a child. Add a driver in a subdirectory that locates the trusted helper relative to its own file.
Next: Bounded recursion and reusable helpers