🧠 5. Bash Internals & Shell Behavior
Estimated reading: 4 minutes 40 views

πŸ” 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 source and . 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β€”source is 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.sh without 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

Featuresource / .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 caseImporting config/functionsRunning 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

PracticeWhy It Helps
Use source for clarityImproves script readability
Check file existence before sourcingAvoids runtime errors
Group functions in lib.sh filesPromotes code reusability
Don’t source untrusted filesPrevents 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:

  • source or . 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

🟒 Bash: Sourcing Files (source, .)

Or Copy Link

CONTENTS
Scroll to Top