source Command
Beginner Shell Scripting man(1)Execute commands from a file in the current shell
👁 8 views
📅 Updated: Mar 15, 2026
SYNTAX
source FILENAME [ARGUMENTS]
What Does source Do?
source (or the dot command .) reads and executes commands from a file in the current shell environment. Unlike running a script with bash script.sh, source does not create a subshell — variables and functions defined in the file become part of your current session.
source is essential for loading environment configuration, applying changes to .bashrc without logging out, loading shell function libraries, and reading .env files for application configuration.
The difference between source file and bash file is critical: source modifies your current environment, while bash file runs in a separate subshell that cannot affect your current shell.
source is essential for loading environment configuration, applying changes to .bashrc without logging out, loading shell function libraries, and reading .env files for application configuration.
The difference between source file and bash file is critical: source modifies your current environment, while bash file runs in a separate subshell that cannot affect your current shell.
Options & Flags
| Option | Description | Example |
|---|---|---|
| source file | Execute file in current shell | source ~/.bashrc |
| . file | POSIX shorthand for source | . ~/.profile |
| with args | Pass arguments to sourced file | source config.sh production |
Practical Examples
#1 Reload shell configuration
Applies changes to .bashrc without logging out and back in.
$ source ~/.bashrc#2 Load environment variables
Loads all variable assignments from .env into the current shell.
$ source .env#3 Load function library
Imports shell functions from a library file for use in the current session.
$ source /usr/lib/bash/functions.sh#4 POSIX dot syntax
The dot command is the POSIX-standard equivalent of source.
$ . ~/.profile#5 Apply Python virtualenv
Activates a Python virtual environment in the current shell.
$ source venv/bin/activate#6 Load secrets file
Loads .env and auto-exports all variables (set -a enables auto-export).
$ set -a; source .env; set +aTips & Best Practices
Auto-export with set -a: set -a before source makes all loaded variables automatically exported. set +a turns it off: set -a; source .env; set +a
source vs bash execution: source modifies YOUR shell. bash script.sh runs in a subshell. If a script sets variables, source it; otherwise the variables are lost.
. is POSIX, source is bash: The . (dot) command works in all POSIX shells. source is a bash extension. For maximum portability, use dot.
Frequently Asked Questions
How do I reload .bashrc without logging out?
Run source ~/.bashrc (or . ~/.bashrc). This re-reads and applies the configuration in your current shell.
What is the difference between source and bash?
source runs commands in your current shell (changes persist). bash runs in a new subshell (changes are lost when it exits).
How do I load a .env file?
Use source .env to load variables. Add set -a before and set +a after to auto-export them: set -a; source .env; set +a
Related Commands
More Shell Scripting Commands
Master Linux with Professional eBooks
Curated IT eBooks covering Linux, DevOps, Cloud, and more
Browse Books →