Bird
Raised Fist0
Bash Scriptingscripting~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using function libraries in bash scripting?
easy
A. To reuse functions across multiple scripts without rewriting code
B. To make scripts run faster by compiling them
C. To encrypt scripts for security
D. To automatically update scripts from the internet

Solution

  1. Step 1: Understand function libraries

    Function libraries contain reusable functions that can be shared across scripts.
  2. Step 2: Identify the benefit

    Reusing functions avoids rewriting code and keeps scripts clean and maintainable.
  3. Final Answer:

    To reuse functions across multiple scripts without rewriting code -> Option A
  4. Quick Check:

    Function reuse = To reuse functions across multiple scripts without rewriting code [OK]
Hint: Function libraries let you reuse code easily [OK]
Common Mistakes:
  • Thinking libraries speed up script execution
  • Confusing libraries with encryption tools
  • Assuming libraries auto-update scripts
2. Which of the following is the correct syntax to source a script named lib.sh in bash?
easy
A. load lib.sh
B. import lib.sh
C. source lib.sh
D. include lib.sh

Solution

  1. Step 1: Recall bash sourcing commands

    Bash uses source or . to load another script.
  2. Step 2: Identify correct syntax

    source lib.sh correctly loads the script named lib.sh.
  3. Final Answer:

    source lib.sh -> Option C
  4. Quick Check:

    source command = source lib.sh [OK]
Hint: Use 'source' or '.' to load scripts in bash [OK]
Common Mistakes:
  • Using 'import' which is not a bash command
  • Using 'include' or 'load' which are invalid in bash
  • Forgetting to specify the script name
3. Given two files:
lib.sh:
function greet() {
  echo "Hello, $1!"
}

main.sh:
source ./lib.sh
greet "Alice"

What will be the output when running bash main.sh?
medium
A. greet: command not found
B. No output
C. Hello, $1!
D. Hello, Alice!

Solution

  1. Step 1: Understand sourcing effect

    The source ./lib.sh command loads the greet function into main.sh.
  2. Step 2: Analyze function call

    Calling greet "Alice" runs the function, echoing "Hello, Alice!".
  3. Final Answer:

    Hello, Alice! -> Option D
  4. Quick Check:

    Function loaded and called = Hello, Alice! [OK]
Hint: Sourcing loads functions so they can be called directly [OK]
Common Mistakes:
  • Not sourcing the library before calling the function
  • Expecting variable $1 to be literal
  • Confusing function definition with execution
4. Consider this script snippet:
source lib.sh
greet "Bob"

But running it gives: main.sh: line 2: greet: command not found
What is the most likely cause?
medium
A. lib.sh does not define the greet function
B. The source command is misspelled
C. The greet function is called before sourcing lib.sh
D. lib.sh has wrong file permissions

Solution

  1. Step 1: Check function definition in lib.sh

    If greet is not defined in lib.sh, sourcing won't load it.
  2. Step 2: Analyze error message

    "command not found" means the function is missing, not a syntax or permission error.
  3. Final Answer:

    lib.sh does not define the greet function -> Option A
  4. Quick Check:

    Missing function definition = lib.sh does not define the greet function [OK]
Hint: Check if function exists in sourced file first [OK]
Common Mistakes:
  • Assuming source command typo without checking
  • Ignoring function order in script
  • Not verifying file permissions
5. You want to create a reusable library 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?
hard
A. In utils.sh: timestamp() { date; } and in run.sh: ./utils.sh; timestamp
B. In utils.sh: timestamp() { echo $(date); } and in run.sh: source utils.sh; timestamp
C. In utils.sh: function timestamp { echo date; } and in run.sh: ./utils.sh; timestamp
D. In utils.sh: timestamp() { date; } and in run.sh: bash utils.sh; timestamp

Solution

  1. Step 1: Define timestamp function correctly

    Use timestamp() { echo $(date); } to print current date and time.
  2. Step 2: Source utils.sh in run.sh and call function

    Use source utils.sh to load the function, then call timestamp.
  3. Final Answer:

    In utils.sh: timestamp() { echo $(date); } and in run.sh: source utils.sh; timestamp -> Option B
  4. Quick Check:

    Correct function and sourcing = In utils.sh: timestamp() { echo $(date); } and in run.sh: source utils.sh; timestamp [OK]
Hint: Use echo $(date) inside function and source before calling [OK]
Common Mistakes:
  • Running utils.sh instead of sourcing it
  • Using echo date instead of echo $(date)
  • Calling function without sourcing library
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard