Skip to content
shellmap

sourceLoad a script into the current shell so its definitions persist across all 5 shells

Equivalents in every shell

Bashunix
source ~/.bashrc

Bash builtin. The POSIX-portable short form is `.` (dot) — `. ~/.bashrc` is equivalent. Use `source` for readability, `.` for POSIX scripts.

Zshunix
source ~/.zshrc

Builtin; identical to bash. Zsh also accepts `.` (POSIX) interchangeably.

Fishunix
source ~/.config/fish/config.fish

Builtin. Fish dropped the `.` alias in 3.0 (deprecated in 2.x) — only `source` works in modern fish.

PowerShellwindows
. .\script.ps1

PowerShell uses dot-sourcing — a leading `.`, a space, then the path. `source` is not a cmdlet. For evaluating a string, see `Invoke-Expression`.

cmd.exewindows
call script.bat

`call` runs another batch file but does NOT share scope by default — variables set inside the called script are local unless you avoid `setlocal`/`endlocal`. There is no true `source` equivalent in cmd.

Worked examples

Reload your shell config after edits

Bash
source ~/.bashrc
Zsh
source ~/.zshrc
Fish
source ~/.config/fish/config.fish
PowerShell
. $PROFILE

Load environment variables from a `.env` file

Bash
set -a; source .env; set +a
Fish
for line in (cat .env); set -gx (string split = $line); end
PowerShell
Get-Content .env | ForEach-Object { $k,$v = $_ -split "=",2; Set-Item "Env:$k" $v }

Define helper functions in a library file and use them in the current shell

Bash
source ./lib.sh && my_func arg
PowerShell
. .\lib.ps1; My-Func arg

Gotchas

  • `source` and `.` are equivalent in POSIX shells, but `.` is whitespace-sensitive: `.script.sh` is parsed as a filename starting with a dot, not as `. script.sh`. Always put a space.
  • PowerShell dot-sourcing requires the dot to be a separate token: `. script.ps1` works, `.script.ps1` does NOT — PowerShell tries to run a binary literally named `.script.ps1`.
  • Scripts that contain `exit 1` (or `exit` at all) will TERMINATE THE INTERACTIVE SHELL when sourced. Always use `return` instead of `exit` in files designed to be sourced.
  • Cmd's `call` is not a true `source`. Variable changes in the called `.bat` are NOT propagated back unless you avoid `setlocal` and stay in the default scope — fragile and rarely worth the effort.
  • Sourcing the same file twice runs it twice — functions/aliases get redefined, exports accumulate (especially `PATH=$PATH:/new/bin` patterns that grow `PATH` unboundedly). Guard with `[ -n "$LIB_LOADED" ] || { LIB_LOADED=1; ...; }` for idempotency.

WSL & PowerShell Core notes

pwshDot-sourcing works identically on Linux/macOS/Windows pwsh. The portable profile path is `$PROFILE`, which resolves to `~/.config/powershell/profile.ps1` on Unix and `Documents/PowerShell/profile.ps1` on Windows.
WSLSourcing `/mnt/c/...` files works but suffers DrvFs overhead. Profile-on-init reads from the WSL home (`/home/$USER`), not the Windows home — keep separate `.bashrc` files per side.

Common tasks using source

Related commands