check if bash script was sourced

how to check if a bash script was sourced in Linux

Added context by specifying 'Linux' and rephrased the query to clarify the user's intent, which will yield more relevant results on checking if a bash script was sourced.

To determine whether a Bash script was sourced or executed directly, you can utilize specific built-in Bash variables. Understanding how to distinguish between these two methods of running a script is crucial, especially when you want to control the behavior of the script based on its execution context.

Understanding Sourcing vs. Executing

When you source a script, it runs in the current shell environment, allowing any functions or variables defined in the script to be available in the current session. Conversely, executing a script runs it in a new shell instance, isolating its environment from the calling shell.

Checking if a Bash Script was Sourced

The key to detecting whether a script was sourced involves comparing two special variables: $0 and ${BASH_SOURCE[0]}.

The Check

You can perform this check by using the following conditional statement within your script:

if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    echo "Script is executed directly"
else
    echo "Script is sourced"
fi

Explanation of the Variables

  • ${BASH_SOURCE[0]}: Contains the path of the script being executed or sourced. When sourcing, it reflects the script's actual path.
  • $0: Represents the name of the current shell or script running. If the script is sourced, $0 will still point to the parent shell, not to the script itself.

Practical Example

Here's a complete example of how you can implement this check in a script:

#!/bin/bash

if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    echo "Executed directly"
else
    echo "Sourced"
fi

Additional Considerations

  • Function Definitions: If your script defines functions you intend to use in the current shell, sourcing is necessary.
  • Exit Behavior: You might want to control whether the script can terminate the shell (for example, using exit statements) based on whether it was sourced or executed directly.

Conclusion

By incorporating this simple check ([[ "${BASH_SOURCE[0]}" == "${0}" ]]) into your Bash scripts, you can easily determine how they are being run. This capability is particularly advantageous for scripts designed to serve dual purposes—either executing in isolation or enhancing the current shell environment when sourced.

Understanding and using these techniques will help you write more robust and flexible Bash scripts tailored to varying execution scenarios.

Related Searches

Sources

10
1
How to Check if Script is Called from Terminal or Sourced from Other ...
Medium

Question: How can you check if a Bash script is being executed directly or sourced? Answer: By comparing ${BASH_SOURCE[0]} with ${0} in an if statement .

2
bash - How to detect if a script is being sourced - Stack Overflow
Stack Overflow

I have a script where I do not want it to call exit if it's being sourced. I thought of checking if $0 == bash but this has problems if the ...

3
Bash: detect execute vs source in a script? - Super User
Superuser

Bash: detect execute vs source in a script? Ask Question. Asked 11 ... (Mac) It seems that BASH_SOURCE is empty when sourced.

4
Detect If a Shell Script Is Being Executed or Sourced - Nick Janetakis
Nickjanetakis

The [[ "${BASH_SOURCE[0]}" == "${0}" ]] condition in the first script is what lets us detect if a script is executed or sourced.

5
Differentiating between running and being sourced in a bash shell ...
Unix

Bash script inheritance? Calling a function from another script? 4 · Sourcing a script containing `echo ${BASH_SOURCE[0]}` still shows the ...

6
Identifying a Bash Script is Sourced or Executed Directly - TecAdmin
Tecadmin

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 ...

7
Bash: test whether script is invoked directly or sourced | Fabian Lee
Fabianlee

If you have a Bash script that needs to be sourced (versus directly executed), you can enforce this by checking the execution context in the script.

8
Confirm if a file is sourced in Bash | Abdulmalik
Blog

Confirm if a file is sourced in Bash · source $1 0 2>/dev/null && sourced=1 || sourced=0 · if [ $sourced -eq 0 ]; then · echo "ERROR, this script ...

9
How to Define a Shell Script to Be Sourced | Baeldung on Linux
Baeldung

This script uses the conditional expression [[ “$0” = “${BASH_SOURCE[0]}” ]] to determine whether we sourced or directly executed it. The $0 ...

10
How do I find the source of a command line operation? - Ask Ubuntu
Askubuntu

You can use the type command, ex. type abc . For example, in a bash shell: $ type while cd ls gcc apt while is a shell keyword cd is a shell ...