exec zsh vs source zshrc
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
~/.zshrcwithin 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
sourcedoesn'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
| Aspect | source ~/.zshrc | exec zsh |
|---|---|---|
| Process | Same process | New process |
| PID | Unchanged | New PID |
| History | Preserved | Reloaded |
| Speed | Faster | Slightly slower |
| State cleanup | Partial | Complete |
| Use case | Quick tweaks | Major 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
- URL: https://zsh.sourceforge.io/Doc/Release/Invocation.html
- Summary: Official zsh documentation on shell initialization and invocation
- Related: Unix, Shell Configuration