6

I just edited the .zshrc file to configure my zsh on FreeBSD.

For example, updating the PATH system variable:

path+=/usr/local/openjdk12/bin

How do I make the changes take effect?

Must I logout and login again? Is there a way to immediately run that file?

  • @Sparhawk If logging in & out is the common approach, post that as an Answer and I will accept it. – Basil Bourque Jul 28 '19 at 6:45
  • Okay. I wasn't sure if I was missing something. I've fleshed it out a little and posted an answer. – Sparhawk Jul 28 '19 at 7:06
  • exec zsh is one way. – Stéphane Chazelas Jul 28 '19 at 7:27
  • I always just do source ~/.zshrc – jamescampbell Nov 13 '19 at 20:05
9

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.
|improve this answer|||||
4

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.

|improve this answer|||||
  • 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. – Stéphane Chazelas Jul 28 '19 at 7:18
3

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.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.