Using Function Libraries by Sourcing Scripts in Bash
📖 Scenario: You are working on a bash script project where you want to reuse some useful functions stored in a separate file. This helps keep your scripts clean and organized, just like having a toolbox with tools ready to use.
🎯 Goal: Learn how to create a bash function library in one script file and then use source to include those functions in another script. Finally, call the functions to see their output.
📋 What You'll Learn
Create a bash script file named functions.sh with two simple functions.
Create a main bash script named main.sh that sources functions.sh.
Call the sourced functions from main.sh and print their outputs.
💡 Why This Matters
🌍 Real World
In real projects, you often keep reusable bash functions in separate files. This helps you avoid rewriting code and keeps your scripts clean.
💼 Career
Knowing how to source scripts and use function libraries is important for system administrators, DevOps engineers, and anyone automating tasks with bash.
Progress0 / 4 steps
1
Create the function library script
Create a bash script file named functions.sh that defines two functions: greet which prints "Hello, friend!" and farewell which prints "Goodbye, friend!". Write the exact function definitions with these names and messages.
Bash Scripting
Hint
Use the syntax function_name() { commands } to define functions in bash.
2
Create the main script and source the function library
Create a bash script file named main.sh. Inside it, use source ./functions.sh to include the functions from functions.sh. This will let you use the functions defined there.
Bash Scripting
Hint
Use source ./functions.sh or . ./functions.sh to include the functions.
3
Call the sourced functions in the main script
In main.sh, after sourcing functions.sh, call the functions greet and farewell by writing their names on separate lines.
Bash Scripting
Hint
Simply write the function names greet and farewell on separate lines to call them.
4
Run the main script and see the output
Run the main.sh script in your terminal. It should print the greeting and farewell messages from the sourced functions. Write a bash main.sh command and observe the output.
Bash Scripting
Hint
Use the command bash main.sh in your terminal to run the script.
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
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 A
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
Step 1: Recall bash sourcing commands
Bash uses source or . to load another script.
Step 2: Identify correct syntax
source lib.sh correctly loads the script named lib.sh.
Final Answer:
source lib.sh -> Option C
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
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
Step 1: Understand sourcing effect
The source ./lib.sh command loads the greet function into main.sh.
Step 2: Analyze function call
Calling greet "Alice" runs the function, echoing "Hello, Alice!".
Final Answer:
Hello, Alice! -> Option D
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
Step 1: Check function definition in lib.sh
If greet is not defined in lib.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 A
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
Step 1: Define timestamp function correctly
Use timestamp() { echo $(date); } to print current date and time.
Step 2: Source utils.sh in run.sh and call function
Use source utils.sh to load the function, then call timestamp.
Final Answer:
In utils.sh: timestamp() { echo $(date); } and in run.sh: source utils.sh; timestamp -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.