How do I set environment variables on Mac zsh?


By default, we don’t have the .zshrc file present in macOS Catalina, and we need to create it. In order to create the .zshrc file, we can follow the steps shown below −

  • Open Terminal

  • Type touch ~/.zshrc to create the file.

  • Hit Return

We can also open the .zshrc file in the terminal from any directory by just typing the command shown below

Example

vi ~/.zshrc

Output

immukul@192 linux-questions-code % cat ~/.zshrc
export GOPATH=/Users/immukul/go_projects
export NDHOME=/Users/immukul/Downloads
export GOTRACEBACK=all
export GOROOT=/usr/local/go
export LC_CTYPE=C
export PATH=/home/Systems
export LANG=C

It should be noted that the output may vary from machine to machine.

To add an entry to the PATH variable present inside the we can write the following command to the terminal, and we don’t even have to open the zshrc file to do that.

Command

echo -n 'export PATH=~/some/path:$PATH' >> ~/.zshrc

Output

immukul@192 linux-questions-code % cat ~/.zshrc
export GOPATH=/Users/immukul/go_projects
export NDHOME=/Users/immukul/Downloads
export GOTRACEBACK=all
export GOROOT=/usr/local/go
export LC_CTYPE=C
export PATH=/home/Systems:/some/path
export LANG=C

Another approach is to open the zshrc file and then insert the export command manually.

Command

export PATH=/home/bin:$PATH

Output

immukul@192 linux-questions-code % cat ~/.zshrc
export GOPATH=/Users/immukul/go_projects
export NDHOME=/Users/immukul/Downloads
export GOTRACEBACK=all
export GOROOT=/usr/local/go
export LC_CTYPE=C
export PATH=/home/Systems:/some/path:/home/bin
export LANG=C

It is recommended to source the file once you are done to make the changes available to all the terminals.

How do I set environment variables on Mac zsh?

Updated on 29-Jul-2021 11:06:32

  • Related Questions & Answers
  • How to set Java Path in Mac OS?
  • How to set Java Path in Linux OS?
  • Recommended IDEs for C# on Windows/Linux/Mac OS
  • How to install Selenium WebDriver on Mac OS?
  • Installing Python on Mac OS
  • What languages have been used to write Windows, Mac OS and Linux OS?
  • How to set your python path on Mac?
  • How to Format a Hard Disk on Linux OS
  • How to compile and execute C# programs on Mac OS?
  • How to set python environment variable PYTHONPATH on Mac?
  • How to add a user to the group on linux
  • How to Add a New Disk Drive to a Linux Machine
  • How to set JAVA_HOME for Java in Mac OS?
  • How to set your python path on Linux?
  • Mac OS X Structure

The operation is the same on both Bash and zsh, with the caveat that to persist them you need to use .bashrc and .zshrc, respectively.

If you edit a dot file, to apply the changes to the current shell use source .dotfile.

This works for Bash and Zsh.

At the end of January I will organize the Web Development Bootcamp.

It's a 10-weeks long cohort online course where I will guide you to becoming a Web Developer.

It's not "just a course". It's a big event I organize once a year.

We'll start from zero, learn the fundamentals of Web Development, HTML CSS, JavaScript, Tailwind, Git, using the command line, VS Code, GitHub, Node.js, we'll then learn React, JSX, how to use PostgreSQL, Astro, Next.js, Prisma, deploying on Netlify/DigitalOcean/Fly/Vercel and much more!

At the end of the first 10 weeks you'll know how to create web sites and web applications and I'll unlock you the 2nd phase of the Bootcamp: you will get access to a large number of projects exclusive to the Bootcamp graduates, so you can follow my instructions to build things like private areas with authentication, clones of popular sites like Twitter YouTube Reddit, create e-commerce sites, and much much more.

Because once you got the fundamentals, you only learn by working on real, exciting projects.

To find out more, visit bootcamp.dev

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 paths and other variables that you need for day-to-day programming or executions, it's always better to add them to 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.

Have Questions? Post them here!

Where is PATH variable stored in Mac zsh?

This file will always be sourced. /etc/zsh/zprofile Used for executing commands at start, will be sourced when starting as a login shell. /etc/profile This file should be sourced by all Bourne-compatible shells upon login: it sets up $PATH and other environment variables and application-specific ( /etc/profile.

Does Mac Have environment variables?

The shell uses environment variables to store information, such as the name of the current user, the name of the host computer, and the default paths to any commands.