Bash scripting is a powerful tool that developers can leverage to automate tasks on Unix and Linux systems. One common use-case is to source a script in another script, allowing the sharing of variables and functions between them. But at times, it’s crucial to know whether a Bash script has been sourced or executed directly. This knowledge can be instrumental in controlling the flow of the program based on its invocation method.
This guide will explore how to identify whether a Bash script is being sourced or executed directly.
Understanding Direct Execution and Sourcing of Bash Scripts
Before we dive into detecting the script invocation method, let’s clarify the difference between sourcing and direct execution of a Bash script.
- Direct Execution: When you run a script directly, the system spawns a new shell process and executes the script within this new shell. Variables and functions defined within the script are only available within the scope of this new shell and will be lost once the script finishes execution.
- Sourcing: On the other hand, if you source a script, it is executed within the same shell. This means any variables or functions that the script defines or modifies will remain available in the shell even after the script has completed execution. The sourcing of scripts is achieved with the source command or the dot operator (.).
Detecting Sourced and Directly Executed Scripts in Bash
To determine if a script is being sourced or run directly, we can use the $0 and BASH_SOURCE variables.
In Bash, `$0` holds the name of the script being executed. When a script is run directly, `$0` will contain the name of the script itself.
`BASH_SOURCE` is an array variable which holds the source history, where `${BASH_SOURCE[0]}` is the source of the current subroutine call, `${BASH_SOURCE[1]}` is the source of the caller of current subroutine, and so on. If a script is sourced, `${BASH_SOURCE[0]}` will contain the name of the script itself.
Let’s look at how to leverage these variables to detect if a script is being sourced or run directly:
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]
then
echo "Script is being run directly."
else
echo "Script is being sourced."
fi
In this code, if the `${BASH_SOURCE[0]}` equals `$0`, then the script is being run directly. Otherwise, it is being sourced.
Implications of Knowing How a Script is Invoked
Knowing how a script is invoked can be useful in a variety of scenarios. For example, you might have a script that can both be sourced for its functions and run directly for its command-line interface. By detecting the invocation method, you can control the behavior of the script accordingly.
Here’s a simple example:
# function definitions...
function1() { ... }
function2() { ... }
# if script is run directly, execute a command
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
function1
function2
fi
In this script, function1 and function2 will only be executed if the script is run directly. If it’s sourced, only the function definitions are processed, and no commands are executed.
Conclusion
Bash provides the tools to determine whether a script is being sourced or run directly. With this knowledge, developers can control the behavior of their scripts based on their invocation method, resulting in more flexible and modular code. As with any programming concept, the best way to solidify your understanding is by practice, so consider how this technique can be applied in your own Bash scripts.