0
0
Bash Scriptingscripting~10 mins

Function libraries (sourcing scripts) in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Function libraries (sourcing scripts)
Start script
Source library script
Library functions loaded
Call library function
Function executes
Continue main script
End script
The main script starts, loads functions from a library script using source, then calls those functions as needed before finishing.
Execution Sample
Bash Scripting
source ./lib.sh
say_hello() {
  echo "Hello from main script"
}
say_hello
greet
This script loads functions from lib.sh, defines its own function, then calls both its own and the sourced function.
Execution Table
StepActionEvaluationResult
1Start main scriptNo outputScript begins
2source ./lib.shLoad functions from lib.shFunction greet() is available
3Define say_hello()Function definedsay_hello() ready to use
4Call say_hello()Runs say_hello functionOutputs: Hello from main script
5Call greet()Runs greet function from lib.shOutputs: Hello from library
6End scriptNo further commandsScript ends
💡 Script ends after all commands run
Variable Tracker
Variable/FunctionStartAfter sourceAfter define say_helloAfter callsFinal
greet (function)Not definedDefined (from lib.sh)DefinedCalled onceDefined
say_hello (function)Not definedNot definedDefinedCalled onceDefined
Key Moments - 3 Insights
Why do we use 'source' instead of just running the library script?
Using 'source' runs the library script in the current shell, so its functions become available to the main script. Running it normally would run in a separate shell and functions wouldn't be accessible.
What happens if the library script is missing or has errors?
The 'source' command will fail, and the main script may stop or show errors when calling missing functions, as seen in step 2 of the execution table.
Can the main script override functions from the library?
Yes, functions defined after sourcing can override library functions. In this example, say_hello is unique, but if it matched greet, the main script's version would be used.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what output appears when calling greet()?
AHello from main script
BHello from library
CNo output
DError: command not found
💡 Hint
Check step 5 in the execution_table where greet() is called
At which step does the greet function become available in the script?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the action 'source ./lib.sh' in the execution_table
If the source command is removed, what happens when greet() is called?
AIt outputs 'Hello from library'
BIt outputs 'Hello from main script'
CIt causes an error: command not found
DIt outputs nothing
💡 Hint
Without sourcing, the function greet() is not defined as shown in variable_tracker
Concept Snapshot
Function libraries in bash use 'source filename' to load functions into the current script.
This allows reuse of code without copying.
Functions in the library become available after sourcing.
Call library functions like normal functions.
If the library is missing, calls to its functions cause errors.
Functions defined later can override library functions.
Full Transcript
This visual execution shows how a bash script uses 'source' to load functions from another script called a library. The main script starts, then runs 'source ./lib.sh' which loads the greet() function into the current shell. Then the main script defines its own function say_hello(). When the script calls say_hello(), it prints 'Hello from main script'. When it calls greet(), it prints 'Hello from library'. The variable tracker shows greet() is not defined before sourcing, but is defined after. Key moments explain why sourcing is needed to share functions, what happens if the library is missing, and how function overriding works. The quiz tests understanding of when functions become available and what outputs appear. This teaches beginners how to organize reusable bash functions in separate files and use them easily.