How to Run a Script in the Current Shell with source
When you run a script the usual way, the terminal starts a separate shell for it, and every variable the script sets disappears when it ends. source runs the script inside your own terminal instead, so what it sets stays. That's how commands like source .venv/bin/activate change your terminal for the rest of the session.
Syntax
In zsh and bash:
source <script>
. <script>
| Part | What it means |
|---|---|
source <script> | Runs the commands in <script> in the current shell |
. <script> | The same thing, under its shorter, older name |
The zsh manual describes . as: "Read commands from file and execute them in the current shell environment." source is the same command.
In PowerShell, the same idea is called dot-sourcing:
. <path to script.ps1>
Microsoft's documentation says it "Runs a script in the current scope so that any functions, aliases, and variables that the script creates are added to the current scope".
The Example
A one-line script sets a variable named GREETING. You run it twice, first normally and then with source, and check after each run whether the variable is still there. The square brackets make an empty result visible.
Step 1: Create the Script
- macOS / Linux
- Windows (PowerShell)
GREETING='Hello from greeting.sh'
$greeting = 'Hello from greeting.ps1'
Step 2: Run It Normally, Then with source
- macOS / Linux
- Windows (PowerShell)
zsh greeting.sh
echo "[$GREETING]"
source greeting.sh
echo "[$GREETING]"
[]
[Hello from greeting.sh]
./greeting.ps1
"[$greeting]"
. ./greeting.ps1
"[$greeting]"
[]
[Hello from greeting.ps1]
![The zsh terminal after running greeting.sh normally and then with source: the first check prints empty brackets, the second prints [Hello from greeting.sh].](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/programming/shell/2026/shell-source-01-zsh.webp)
The normal run set GREETING in a separate shell that ended with it, so your terminal saw nothing: []. With source, the line ran in your own terminal, and the variable stayed. In bash, bash greeting.sh behaves like zsh greeting.sh here.
Where You'll Meet It
Python virtual environments are switched on with source .venv/bin/activate. The activate script changes your terminal's PATH so python means the environment's Python. Run without source, it would change a shell that ends straight away, and nothing would happen.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Running a settings script as ./script.sh or zsh script.sh | Its variables and cd don't reach your terminal | source script.sh |
source in PowerShell | The term 'source' is not recognized as a name of a cmdlet, function, script file, or executable program. | . ./script.ps1, with a space after the first dot |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| A script "did nothing" to your terminal | It ran in a separate shell | Run it again with source |
A virtual environment isn't active after running activate | It was run, not sourced | source .venv/bin/activate |
FAQ
Is . the same as source?
Yes, in zsh and bash. . is the older, standard name, and source is easier to read.
Is sourcing a script safe?
It runs every line in your own terminal, with your permissions, so only source scripts you trust.
Conclusion
Running greeting.sh normally left GREETING empty, and source greeting.sh kept it in the terminal. Use source, or . in PowerShell, for scripts whose job is to change your current terminal.
Additional Resources
- zsh builtins —
.andsource. - about_Operators — PowerShell's dot sourcing operator.
