Skip to main content

How to Run a Script in the Current Shell with source

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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:

Syntax
source <script>
. <script>
PartWhat 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:

Syntax
. <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​

greeting.sh
GREETING='Hello from greeting.sh'

Step 2: Run It Normally, Then with source​

Terminal
zsh greeting.sh
echo "[$GREETING]"
source greeting.sh
echo "[$GREETING]"
Output
[]
[Hello from greeting.sh]

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].

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​

MistakeWhat happensDo this instead
Running a settings script as ./script.sh or zsh script.shIts variables and cd don't reach your terminalsource script.sh
source in PowerShellThe 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​

SymptomLikely causeFix
A script "did nothing" to your terminalIt ran in a separate shellRun it again with source
A virtual environment isn't active after running activateIt was run, not sourcedsource .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​