How to add permanent environment variable in zsh I have been scouring the internet, and cannot seem to find a solution to permanently adding an environment variable, specifically when my OS is macOS Mojave (10.14).  It seems there are a lot of tutorials for past versions of the OS, but none for this one one.  It also seems every old method has become outdated.

I want to add an environment variable ENV_VAR=12345 to my Mac, so that I can import it into a Python module using os.environ['ENV_VAR']

The most relevant tutorial I have found is this, but it doesn't quite do the trick for me. A lot of others tell you how to temporarily add environment variables to bash, but I don't think this is good enough.  I want the addition to be there if you restart terminal.

Can you please either provide a short tutorial or point me to the correct/modern tutorial?

UPDATE: I should have mentioned that I use zsh.  This was key.
Bash

Since Bash is typically the default shell you can open up this file in your home directory:

$ vim ~/.bash_profile


And add your variable to this file:

export ENV_VAR=12345


You can do this without even having to edit this file if you like, using the following one-liner:

$ echo 'export ENV_VAR=12345' &gt;&gt; ~/.bash_profile


And then confirm like so:

$ cat ~/.bash_profile
for i in ~/.bash_profile.d/[0-9]*; do
  . "$i"
done
export ENV_VAR=12345


After doing the above, if you open a new terminal you should see that environment variable has been set:

$ echo $ENV_VAR
12345


Zsh

If you find that you're using an alternative shell such as zsh, that uses a different set of configuration files maintained within your home directory, ~. Luckily the syntax of the changes is basically the same, just different files. So you can add the above example to this file instead:

$ echo 'export ENV_VAR=12345' &gt;&gt; ~/.zshenv


And then when you launch a zsh:

$ echo $ENV_VAR
12345


References


Zsh Startup Files