source — Load a script into the current shell so its definitions persist across all 5 shells
Equivalents in every shell
source ~/.bashrcBash builtin. The POSIX-portable short form is `.` (dot) — `. ~/.bashrc` is equivalent. Use `source` for readability, `.` for POSIX scripts.
source ~/.config/fish/config.fishBuiltin. Fish dropped the `.` alias in 3.0 (deprecated in 2.x) — only `source` works in modern fish.
. .\script.ps1PowerShell uses dot-sourcing — a leading `.`, a space, then the path. `source` is not a cmdlet. For evaluating a string, see `Invoke-Expression`.
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
source ~/.bashrcsource ~/.zshrcsource ~/.config/fish/config.fish. $PROFILELoad environment variables from a `.env` file
set -a; source .env; set +afor line in (cat .env); set -gx (string split = $line); endGet-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
source ./lib.sh && my_func arg. .\lib.ps1; My-Func argGotchas
- `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
Common tasks using source
- Check if a script is sourced or executed
Detect at runtime whether the current script was sourced into the parent shell (`. script` / `source script`) or executed in a subshell (`bash script`) — so the same file can be used both as a library and as a CLI.
- Source an env file into the current shell
Load variables from a `.env`-style file into the current shell session — for project-specific secrets, dev-mode flags, and tool-version locks.