π Bash: Sourcing Files β Use source and . to Load Scripts Without Starting a New Shell
π§² Introduction to Sourcing in Bash β Reuse Scripts with source or .
Bash allows you to load and execute another script into the current shell using the source command or its shorthand . (dot). This process is called sourcing, and itβs useful when you want to reuse variables, functions, or environment settings defined in another file without launching a new shell process.
Unlike running a script with ./script.sh or bash script.sh, sourcing keeps everything in the same shell session.
π― In this article, youβll learn:
- What
sourceand.do in Bash - The difference between sourcing and executing
- When and why to use sourcing
- Real-world examples using environment variables and function libraries
β‘ What Does source Do in Bash?
The source command reads and executes the contents of a file in the current shell, not in a subshell.
π Syntax:
source filename
or simply:
. filename
π§ Both forms are identicalβ
sourceis more readable;.is more compact.
π§ͺ Example 1: Source a File with Variable Definitions
# config.sh
NAME="Vaibhav"
ENV="production"
# main.sh
source config.sh
echo "Running as $NAME in $ENV mode"
β Output:
Running as Vaibhav in production mode
π These variables are now available in
main.shwithout redefinition.
π§ͺ Example 2: Source a File with Functions
# utils.sh
log() {
echo "[INFO] $1"
}
# main.sh
source utils.sh
log "Script started"
β Output:
[INFO] Script started
π¦ Sourcing is great for modular script designβsplit functions into reusable files.
π§ͺ Example 3: Use . Instead of source
. ./config.sh
β
Same effect as source config.sh.
π Sourcing vs Executing a Script
| Feature | source / . | bash script.sh or ./script.sh |
|---|---|---|
| Same shell? | β Yes | β No (runs in a subshell) |
| Shares variables? | β Yes | β No (variables are isolated) |
| Keeps environment? | β Yes | β No (child process doesn’t persist) |
| Typical use case | Importing config/functions | Running standalone scripts |
β οΈ When to Use Sourcing (and When Not To)
β Use Sourcing:
- To import functions, aliases, or env variables
- In interactive sessions to apply profile changes (e.g.,
.bashrc) - When sharing logic across multiple scripts
β Avoid Sourcing:
- If the file modifies shell behavior destructively
- If isolation is needed (e.g., you donβt want its variables/functions to persist)
π§ Best Practices for Sourcing
| Practice | Why It Helps |
|---|---|
Use source for clarity | Improves script readability |
| Check file existence before sourcing | Avoids runtime errors |
Group functions in lib.sh files | Promotes code reusability |
| Don’t source untrusted files | Prevents malicious code execution |
π§ͺ Example: Check Before Sourcing
CONFIG_FILE="./config.sh"
if [[ -f "$CONFIG_FILE" ]]; then
source "$CONFIG_FILE"
else
echo "Missing config file!"
exit 1
fi
π Summary β Bash source and . Usage
The source and . commands let you reuse shell codeβvariables, functions, configurationsβwithout running a new process. This enables modular, DRY Bash scripting practices across scripts and environments.
π Key Takeaways:
sourceor.executes code in the current shell session- Great for importing variables, functions, and settings
- Does not start a subshell like
bash script.sh - Helps split scripts into maintainable, reusable modules
βοΈ Real-world Uses:
- Loading config files in deployment scripts
- Sharing utility functions across scripts
- Setting up interactive shell environments
β FAQ β Bash source and . Usage
β Is source the same as . in Bash?
β
Yes. source filename and . filename are functionally equivalent in Bash.
β Can I source a script from another directory?
β
Yes:
source /path/to/script.sh
β What happens if I run a script with bash script.sh instead of source?
β
A new shell runs the script, and changes (like variables) wonβt affect the current shell.
β How do I check if a file has already been sourced?
β
Use a marker variable:
[[ -z "$CONFIG_LOADED" ]] && source config.sh && CONFIG_LOADED=1
β Can I use source in a cron job?
β
Not directly recommendedβcron runs non-interactively. Instead, set variables inside the script, not via sourced files unless full paths and environments are guaranteed.
Share Now :
