Function libraries (sourcing scripts) in Bash Scripting - Time & Space Complexity
When using function libraries by sourcing scripts in bash, it is important to understand how the execution time grows as the size of the library increases.
We want to know how sourcing affects the script's running time as more functions are added.
Analyze the time complexity of the following code snippet.
# source a library of functions
source my_functions.sh
# call a function from the library
my_function "argument"
# continue with script tasks
This code sources a file containing many functions, then calls one function from it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading and loading all lines of the sourced script once.
- How many times: Exactly once per script execution, no loops or repeated traversals.
As the size of the function library file grows, the time to read and load it grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 lines | 10 operations to read and load |
| 100 lines | 100 operations to read and load |
| 1000 lines | 1000 operations to read and load |
Pattern observation: The time grows linearly as the library size increases.
Time Complexity: O(n)
This means the time to source the script grows directly with the number of lines or functions in the library.
[X] Wrong: "Sourcing a script only loads the functions I call, so time does not grow with library size."
[OK] Correct: When sourcing, the entire file is read and parsed, even if only one function is used later.
Understanding how sourcing affects script performance helps you write efficient bash scripts and shows you can reason about script behavior beyond just coding.
"What if the sourced script used lazy loading or conditional function definitions? How would the time complexity change?"
Practice
Solution
Step 1: Understand function libraries
Function libraries contain reusable functions that can be shared across scripts.Step 2: Identify the benefit
Reusing functions avoids rewriting code and keeps scripts clean and maintainable.Final Answer:
To reuse functions across multiple scripts without rewriting code -> Option AQuick Check:
Function reuse = To reuse functions across multiple scripts without rewriting code [OK]
- Thinking libraries speed up script execution
- Confusing libraries with encryption tools
- Assuming libraries auto-update scripts
lib.sh in bash?Solution
Step 1: Recall bash sourcing commands
Bash usessourceor.to load another script.Step 2: Identify correct syntax
source lib.shcorrectly loads the script namedlib.sh.Final Answer:
source lib.sh -> Option CQuick Check:
source command = source lib.sh [OK]
- Using 'import' which is not a bash command
- Using 'include' or 'load' which are invalid in bash
- Forgetting to specify the script name
lib.sh:
function greet() {
echo "Hello, $1!"
}
main.sh:
source ./lib.sh
greet "Alice"What will be the output when running
bash main.sh?Solution
Step 1: Understand sourcing effect
Thesource ./lib.shcommand loads thegreetfunction intomain.sh.Step 2: Analyze function call
Callinggreet "Alice"runs the function, echoing "Hello, Alice!".Final Answer:
Hello, Alice! -> Option DQuick Check:
Function loaded and called = Hello, Alice! [OK]
- Not sourcing the library before calling the function
- Expecting variable $1 to be literal
- Confusing function definition with execution
source lib.sh greet "Bob"
But running it gives:
main.sh: line 2: greet: command not foundWhat is the most likely cause?
Solution
Step 1: Check function definition in lib.sh
Ifgreetis not defined inlib.sh, sourcing won't load it.Step 2: Analyze error message
"command not found" means the function is missing, not a syntax or permission error.Final Answer:
lib.sh does not define the greet function -> Option AQuick Check:
Missing function definition = lib.sh does not define the greet function [OK]
- Assuming source command typo without checking
- Ignoring function order in script
- Not verifying file permissions
utils.sh with a function timestamp that prints the current date and time. How should you write and use it in your script run.sh to print the timestamp?Solution
Step 1: Define timestamp function correctly
Usetimestamp() { echo $(date); }to print current date and time.Step 2: Source utils.sh in run.sh and call function
Usesource utils.shto load the function, then calltimestamp.Final Answer:
In utils.sh: timestamp() { echo $(date); } and in run.sh: source utils.sh; timestamp -> Option BQuick Check:
Correct function and sourcing = In utils.sh:timestamp() { echo $(date); }and in run.sh:source utils.sh; timestamp[OK]
- Running utils.sh instead of sourcing it
- Using echo date instead of echo $(date)
- Calling function without sourcing library
Ready to prove it?
Pass the test and this pattern moves unknown
