Bash source Command: Load Scripts and Variables

The source command reads and executes commands from the file specified as its argument in the current shell environment. It is useful for loading functions, variables, and configuration files into shell scripts.
source is a shell built-in in Bash and other popular shells used in Linux and UNIX operating systems. Its behavior may be slightly different from shell to shell.
Syntax
The syntax for the source command is:
source FILENAME [ARGUMENTS]
. FILENAME [ARGUMENTS]sourceand.(a period) are the same command. The dot form is POSIX-compliant and works in any POSIX shell.- If
FILENAMEis not a full path, the command searches for the file in the directories listed in the$PATHenvironment variable . If the file is not found in$PATH, it looks in the current directory. - If
ARGUMENTSare provided, they become the positional parameters of the sourced file. - The exit code
is
0if the file is found and executed, or1if the file does not exist.
source vs. Running a Script Directly
The key difference between source script.sh and bash script.sh is which shell environment the commands run in:
bash script.sh— spawns a new subshell, runs the script there, and discards all variables and functions when the subshell exits. The parent shell is unchanged.source script.sh— runs the script in the current shell. Any variables, functions, or environment changes defined in the script remain available after it finishes.
This distinction is the core reason source exists. If you want a script to set variables that your current session can use, you must source it.
Reload Shell Configuration
The most common use of source is reloading the shell configuration file after making changes, without needing to open a new terminal:
source ~/.bashrcAfter editing ~/.bashrc to add aliases
, functions, or environment variables, run the command above to apply the changes immediately. Without source, you would need to log out and log back in.
For login shells, reload the profile instead:
source ~/.bash_profileSee .bashrc vs .bash_profile for an explanation of when each file is used.
Sourcing Functions
If multiple scripts use the same functions , extract them into a shared file and source it where needed. This avoids duplicating code across scripts.
In the following example, we create a file that contains a function to check whether the script is running as root:
check_root () {
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
}In each script that requires root, source the file and call the function:
#!/usr/bin/env bash
source functions.sh
check_root
echo "I am root"If the script runs as a non-root user, it prints the message and exits. The advantage of this approach is that your scripts stay smaller and more readable, the shared function file is maintained in one place, and any update to it is automatically picked up by all scripts that source it.
Read Variables from a File
source can also read variables from a configuration file. The variables must use Bash assignment syntax: VARIABLE=VALUE.
Create a configuration file:
VAR1="foo"
VAR2="bar"In your script, source the file to load the variables:
#!/usr/bin/env bash
source config.sh
echo "VAR1 is $VAR1"
echo "VAR2 is $VAR2"Running the script produces:
VAR1 is foo
VAR2 is barThis pattern is useful for keeping configuration separate from logic, making scripts easier to maintain and reuse across different environments.
Quick Reference
| Task | Command |
|---|---|
| Source a file | source filename.sh |
| Source using dot shorthand | . filename.sh |
Reload .bashrc | source ~/.bashrc |
Reload .bash_profile | source ~/.bash_profile |
| Source with arguments | source filename.sh arg1 arg2 |
Troubleshooting
source: filename: not found
Use an explicit path such as source ./filename.sh and confirm the file exists with ls -l.
Variables are not loaded after sourcing
Check the sourced file for valid Bash assignments (VAR=value with no spaces around =) and syntax errors.
Script exits right after source file.sh
The sourced file may contain exit, which terminates the current shell or script. Use return in sourced helper files.
FAQ
What is the difference between source and . (dot)?
They are identical. . is the POSIX standard form and works in any POSIX-compliant shell. source is a Bash built-in that does the same thing. Use . in scripts intended to run in /bin/sh, and source in Bash-specific scripts for readability.
Why do I need source to reload .bashrc?
Because running bash ~/.bashrc would execute it in a subshell. Any variables or aliases defined in it would be discarded when that subshell exits. Using source ~/.bashrc runs it in your current session, so the changes take effect immediately.
Can I pass arguments to a sourced file?
Yes. Any arguments after the filename become positional parameters ($1, $2, etc.) inside the sourced file: source script.sh arg1 arg2.
What happens if the file does not exist?source returns exit code 1 and prints an error. In a script, you can handle this with: source file.sh || echo "file not found".
What is the difference between source and import in other languages?source is similar in concept — it loads and executes another file. The key difference is that source is not a module system; it simply runs the file inline in the current shell, with no namespacing or isolation.
Conclusion
The source command runs a file in the current shell environment, making its variables and functions available to the calling session or script. Use it to reload shell configuration files, share functions across scripts, and load variables from configuration files.
If you have any questions, feel free to leave a comment below.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page