Quick take: The source command (or its shorthand .) runs a script in the current shell instead of a new one, so any variables, functions, or directory changes it makes persist. source ~/.bashrc reloads your shell configuration.
Introduction
Normally, running a script starts a new shell that exits when the script finishes, discarding any environment changes it made. The source command runs the script in your current shell instead, so its variables, functions, and aliases stick around. This is why you source ~/.bashrc to apply configuration changes without reopening the terminal.
Syntax
The basic syntax of the source command is:
source FILE [ARGUMENTS]
. FILE [ARGUMENTS]Common Options and Parameters
The most useful options and parameters for the source command:
| Option | Description |
|---|---|
| source FILE | Execute a file in the current shell. |
| . FILE | POSIX shorthand for source (a single dot). |
| source FILE args | Pass positional arguments to the sourced script. |
Practical Examples
Real source commands you can run today:
# Reload your shell configuration
source ~/.bashrc
# Shorthand with the dot
. ~/.bashrc
# Load environment variables from a file
source .env
# Load functions defined in a script
source ./functions.sh
# Activate a Python virtual environment
source venv/bin/activateTips and Best Practices
- Use
sourcewhenever a script must change your shell — set variables, define functions, or change directory — because a normal run cannot affect the parent shell. - After editing
~/.bashrcor~/.profile,sourceit to apply changes without logging out. - Activating Python virtual environments relies on source —
source venv/bin/activatemodifies your current shell's PATH.
Final Thoughts
source runs a script in your current shell so its environment changes persist — the mechanism behind reloading ~/.bashrc, loading .env files, and activating virtual environments. Remember the single-dot shorthand (. file), and reach for source whenever a script needs to change the shell you are sitting in, not a throwaway child shell.
FAQ: source Command in Linux
What does the source command do?+
source runs a script in the current shell rather than a new one, so any variables, functions, aliases, or directory changes it makes persist in your session.
What is the difference between source and running a script?+
Running a script (./script.sh) starts a separate shell that exits when done, discarding its environment changes. source runs the script in your current shell, so its changes remain.
How do I reload .bashrc without restarting the terminal?+
Run source ~/.bashrc (or . ~/.bashrc). This re-reads the file in your current shell so new aliases, variables, and functions take effect immediately.
What is the dot command?+
A single dot is the POSIX shorthand for source: . file is equivalent to source file. It runs the file in the current shell.
Why does activating a virtualenv use source?+
Activating a Python virtual environment changes your shell's PATH and variables. That only works if the activate script runs in your current shell, which is why you use source venv/bin/activate.
Need help with Linux servers or infrastructure?
Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.
Hire Me for Support