C.W.K.
Stream
Lesson 02 of 10 · published

Shell Configuration Files

~13 min · zshrc, zprofile, zshenv, bashrc

Level 0Window Tourist
0 XP0/95 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Why are there so many of them?

zsh reads up to four config files at startup. Each has a different purpose. Putting the right line in the wrong file is the most common "my env doesn't load" bug.

The four zsh files

  • ~/.zshenv — runs for every zsh invocation, including non-interactive scripts. Use sparingly.
  • ~/.zprofile — login shells only (the first shell of a session). Set $PATH here. Homebrew shellenv lives here.
  • ~/.zshrc — interactive shells. Aliases, prompts, plugins, completion. Most of your config goes here.
  • ~/.zlogin — login shells, after .zshrc. Rarely used.

The bash equivalents

  • ~/.bash_profile — login shells.
  • ~/.bashrc — interactive non-login shells.
  • Old hack: source one from the other so you only edit .bashrc.

Login vs interactive (preview)

Login shell = first shell of a session (Terminal opens, ssh, su -). Interactive = you can type at a prompt. Most macOS Terminal sessions are both; ssh sessions are too. Subshells (bash from inside zsh) are interactive but not login. Next lesson covers the table.

Where to put what

  • PATH and locale → .zprofile (so non-interactive scripts via launchd see them too).
  • Aliases, functions, prompt → .zshrc.
  • Variables you want every script to see → .zshenv (rare).

Reload after editing

source ~/.zshrc or exec zsh reloads. The second is cleaner — it replaces the current shell with a fresh one and preserves your terminal window.

Code

Verify the load order·bash
# Add a print to each file so you can see when each runs
echo 'echo ".zshenv loaded"'   >> ~/.zshenv
echo 'echo ".zprofile loaded"' >> ~/.zprofile
echo 'echo ".zshrc loaded"'    >> ~/.zshrc
# Open a new terminal — note the order
# Then remove these lines once you've seen them
Reload without closing the terminal·bash
exec zsh           # cleanest
source ~/.zshrc    # also fine for interactive only
# After editing PATH / .zprofile, prefer exec zsh

External links

Exercise

Add a echo print to each of .zshenv, .zprofile, .zshrc. Open a new Terminal window. Watch the order of prints. Then run bash -c 'echo from-bash; ssh-keygen --help' to see how a non-login shell loads (or doesn't load) those files. Remove the prints when done.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.