source runs the commands in a file inside the shell you’re typing in, so anything the file sets, such as a variable, is still set afterwards. Running the same file with zsh starts a separate shell, which forgets its variables when it ends.
Every command and output on this page is real, run in zsh on macOS.
Syntax
Syntax
source <file>
source: zsh’s command for running a file’s commands in the current shell.<file>: the path to the file, such asgreet.shin the current folder.
See source in the zsh manual’s Shell Builtin Commands.
The Example
A one-line file, greet.sh, that sets a variable named NAME.
Step 1: Write the File
greet.sh
NAME=Asha
Step 2: Run It Without source
Run the file with zsh, then print a greeting that uses NAME:
Terminal
zsh greet.sh
echo "Hello, $NAME"
Output
Hello,
zsh greet.sh ran the file in a separate shell, so NAME was gone when it finished.
Step 3: Run It with source
Terminal
source greet.sh
echo "Hello, $NAME"
Output
Hello, Asha
This time the file ran in your own shell, so NAME is still set.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
A wrong file name: source greeting.sh | zsh stops: source: no such file or directory: greeting.sh | Check the file’s name and the folder you’re in |
FAQ
Where will I see source?
When you activate a Python virtual environment: source .venv/bin/activate runs the environment’s activate script in your shell, so the environment stays on for the commands you type next.
Does a variable set with source last forever?
No. It lasts until you close that Terminal window or tab. A new window starts a new shell without it.
Conclusion
Use source when a file must change your own shell, for example to set variables. Running the file with zsh changes only a separate shell, which ends with the file.
Additional Resources
- Shell Builtin Commands (zsh manual)
