Permanently set $PATH variable in Mac Zsh shell


You must have noticed that as soon as you exit your Mac Terminal, the environment variable such as $PATH is not available. The $PATH was available until the Terminal session was active, but soon as it was terminated, we do not have access to the variable. In order to set the $PATH variable permanently, you would need to set it in the .zshrc file (Z-Shell resource file).

What is .zshrc file?

.zshrc is a Zsh shell resource file. This file runs whenever you start zsh shell. So all the commands and scripts you write in this file are executed as soon as you launch the terminal. So if you want to set path and other variables that you need for day-to-day programming or executions, its always better to add them in this file.

How to add $PATH in .zshrc file?

  1. Open Terminal,
  2. Make sure you are on Zsh Shell, if not type zsh and hit enter,
  3. Now type nano ~/.zshrc
  4. Now add your $PATH variable details: Example: export PATH="$PATH:/opt/homebrew/bin/"
  5. Press Control + X, followed by Y to save the file, press enter to exit Nano,
  6. Now type command source ~/.zshrc to apply changes.
Permanenlty set PATH variable in Mac Zsh Shell
Permanenlty set PATH variable in Mac Zsh Shell

Now even when you close your terminal and open it or switch between prompts, the Zsh shell will always have your $PATH variable set and it will not get lost.