← Back to articles

exec zsh vs source zshrc

Path: Computer Tech/System Configuration/Unix/zsh/exec zsh vs source zshrc.mdUpdated: 2/3/2026

exec zsh vs source zshrc

Two common ways to reload zsh configuration after making changes to ~/.zshrc, but they work fundamentally differently.

source ~/.zshrc

What it does:

  • Reloads configuration in the current shell process
  • Re-executes all commands in ~/.zshrc within existing environment
  • Updates environment variables, aliases, functions

Process behavior:

  • Same Process ID (PID)
  • Shell history preserved
  • Terminal state maintained
  • Faster execution

Use when:

  • Making small config tweaks during development
  • Testing new aliases or environment variables
  • You want to keep current terminal state

Example:

bash
# Edit your zshrc
nvim ~/.zshrc

# Reload config
source ~/.zshrc

# Same shell, PID unchanged
echo $$  # Shows same process ID as before

exec zsh

What it does:

  • Replaces the current shell process with a completely new one
  • Terminates existing shell and starts fresh
  • Clears all process state

Process behavior:

  • New Process ID (PID) assigned
  • Shell history context temporarily lost (until new history loads)
  • Complete environment reset
  • Prompt fully reinitialized

Use when:

  • After major structural changes (new config files, directory reorganization)
  • Fixing broken shell state or syntax errors
  • source doesn't fully apply changes
  • Want guaranteed clean slate

Example:

bash
# Edit your zshrc
nvim ~/.zshrc

# Replace current shell with new process
exec zsh

# New shell, different PID
echo $$  # Shows new process ID

Key Differences

Aspectsource ~/.zshrcexec zsh
ProcessSame processNew process
PIDUnchangedNew PID
HistoryPreservedReloaded
SpeedFasterSlightly slower
State cleanupPartialComplete
Use caseQuick tweaksMajor changes

When Each Method Matters

source is sufficient for:

  • Adding new aliases
  • Changing environment variables
  • Updating PATH
  • Loading new functions

exec is necessary for:

  • Fixing broken completions (syntax errors)
  • After restructuring config files
  • When source produces errors
  • Clearing lingering problematic state

Real-world Example

When transitioning from config/zshrc+/ to shell-config/zsh/:

bash
# Major structural change - use exec
exec zsh

# Verify changes took effect
echo $EDITOR  # Should show 'nvim'

If you had used source, old broken bun completions might have left error state in the environment. exec guarantees a clean start.

Alias Shortcuts

Common aliases for quick reloading:

bash
# In ~/.zshrc or shell-config/zsh/aliases.zsh
alias zshconfig='nvim ~/.zshrc'
alias zshreload='source ~/.zshrc'
alias zshrestart='exec zsh'

Links

Zsh Documentation - Invocation