Sourcing a script means running it in the current shell session so that functions and variables defined in it become available immediately without starting a new shell.
Click to reveal answer
beginner
How do you source a script named library.sh in Bash?
Use the command source library.sh or the shorthand . library.sh to include the script in the current shell session.
Click to reveal answer
beginner
Why use function libraries in Bash scripting?
Function libraries let you reuse code easily by keeping common functions in one file. You can source this file in many scripts to avoid rewriting the same code.
Click to reveal answer
intermediate
What happens if you run a script normally instead of sourcing it when you want to use its functions?
Running a script normally starts a new shell, so functions and variables defined inside it won't be available in your current shell session.
Click to reveal answer
intermediate
How can you check if a function library script was sourced successfully?
After sourcing, try calling a function from the library. If it runs without error, the sourcing worked. You can also check if the function is defined using type function_name.
Click to reveal answer
Which command correctly sources a script named utils.sh?
Abash utils.sh
B./utils.sh
Crun utils.sh
Dsource utils.sh
✗ Incorrect
The source command runs the script in the current shell, making its functions and variables available.
What is the main benefit of sourcing a function library script?
AIt deletes the script after running.
BIt runs the script in a new shell.
CIt makes functions available in the current shell.
DIt compiles the script.
✗ Incorrect
Sourcing runs the script in the current shell, so functions and variables become accessible immediately.
If you run ./library.sh instead of sourcing it, what happens to the functions defined inside?
AThey become available in the current shell.
BThey are only available in the new shell started by the script.
CThey are deleted.
DThey run automatically in the current shell.
✗ Incorrect
Running the script starts a new shell, so functions stay local to that shell and do not affect the current one.
Which symbol is a shorthand for the source command in Bash?
A.
B&
C#
D$
✗ Incorrect
The dot . is a shorthand for source in Bash.
How can you verify if a function named greet is defined after sourcing a script?
Atype greet
Brun greet
Ccheck greet
Dfind greet
✗ Incorrect
The type command shows if a function or command is defined in the current shell.
Explain what sourcing a script means and why it is useful in Bash scripting.
Think about how sourcing affects your current shell session.
You got /4 concepts.
Describe the difference between running a script normally and sourcing it, especially regarding function availability.
Consider where the functions live after each method.
You got /4 concepts.
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.