I just edited the .zshrc file to configure Z shell on FreeBSD, for example to update the PATH system variable.


How do I make the changes take effect?

Must I log out and log in again? Is there a way to immediately run that file?


4 Answers 4


Restart zsh

Zsh reads .zshrc when it starts. You don't need to log out and log back in. Just closing the terminal and opening a new one gives you your new .zshrc in this new terminal. But you can make this more direct. Just tell zsh to relaunch itself:

exec zsh

If you run this at a zsh prompt, this replaces the current instance of zsh by a new one, running in the same terminal. The new instance has the same environment variables as the previous one, but has fresh shell (non-exported) variables, and it starts a new history (so it'll mix in commands from other terminals in typical configurations). Any background jobs are disowned.

Reread .zshrc

You can also tell zsh to re-read .zshrc. This has the advantage of preserving the shell history, shell variables, and knowledge of background jobs. But depending on what you put in your .zshrc, this may or may not work. Re-reading .zshrc runs commands which may not work, or not work well, if you run them twice.

. ~/.zshrc

There are just too many things you can do to enumerate everything that's ok and not ok to put in .zshrc if you want to be able to run it twice. Here are just some common issues:

  • If you append to a variable (e.g. fpath+=(~/.config/zsh) or chpwd_functions+=(my_chpwd)), this appends the same elements again, which may or may not be a problem.
  • If you define aliases, and also use the same name as a command, the command will now run the alias. For example, this works:
    function foo { … }
    alias foo='foo --common-option'
    But this doesn't, because the second time the file is sourced, foo () will expand the alias:
    foo () { … }
    alias foo='foo --common-option'
  • If you patch an existing zsh function, you'll now be patching your own version, which will probably make a mess.
  • If you do something like “swap the bindings of two keys”, that won't do what you want the second time.

Changes to a shells initialisation files will be active in the next shell that you start, for example if you bring up a new graphical terminal or log out and in again. If you've made changes that should affect your desktop environment in some way (I don't know what kind of change that may be), then logging out and in again would be required.

You could source the file with . /path/to/filename (. ~/.zshrc in your case) or start a new shell session from the command line with zsh, but this is almost never a good idea as it may have unwanted consequences such as adding duplicate paths to the $PATH variable or starting extra ssh-agent processes or whatever it is you may be doing in that file. The changes would also not be visible program that have already been started.

For a change such as just adding to the $PATH unconditionally, you could obviously just run the added command in the current shell:

$ path+=/usr/local/openjdk12/bin

This would (in zsh) add the /usr/local/openjdk12/bin directory to the end of $PATH (and to the end of the $path array in zsh) in the current shell session. Again, this change to $PATH would not affect already running processes.

  • 2
    Those unwanted consequences would also be triggered if the OP just started a new zsh shell. It's customary to do typeset -U path in zsh so the $PATH entries remain always unique. $PATH is better set in ~/.zprofile though. Jul 28, 2019 at 7:18
  • @StéphaneChazelas "$PATH is better set in ~/.zprofile though." Why is that? May 26, 2021 at 10:35
  • @MarlonRichert, because it's more of a login session setting than a shell settings though those distinctions are not as clear these days that you don't always login via a shell. May 26, 2021 at 12:32

You could source the new file, which would work for some changes, possibly including updating the PATH variable (depending on other lines). However, sourcing it would simply run .zshrc again, and you might execute unexpected duplicate commands. Moreover, if there were deleted lines from the old .zshrc, then they wouldn't be "erased" from the session.

The cleanest way is to just log out and in again. You'd only need to do it for the terminal session, not the whole desktop environment.


Oh My Zsh

Oh My Zsh is an open-source, community-driven framework for managing your Zsh configuration.

After installation, you can run the following command:

omz reload

to reload the .zshrc configuration file.

  • 2
    This effectively runs exec zsh, which is already mentioned in the accepted answer.
    – Kusalananda
    May 31 at 10:47
  • @Kusalananda thanks for clarifying that. I thought it doesn't actually restart zsh, so you don't lose anything session related, like variables. But I guess I was wrong. I tried to search on the web for what omz reload does, but I couldn't find anything more specific than "it reloads the configuration".
    – tkalvin
    Jun 1 at 14:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .