0
0
Bash Scriptingscripting~5 mins

Function libraries (sourcing scripts) in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does it mean to 'source' a script in Bash?
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
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.
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.
Which symbol is a shorthand for the source command in Bash?
A.
B&
C#
D$
How can you verify if a function named greet is defined after sourcing a script?
Atype greet
Brun greet
Ccheck greet
Dfind greet
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.