21

What is the proper (2021 way) of creating a permanent environment variable on a Mac (macOS Big Sur) and then use it within a Java project.

There are many very old posts regarding this topic. None of them seem to work properly nowadays.

  1. How to add a permanent environment value (through terminal)?
  2. And how can I use it in a Java code?

I'm also not sure how I was able to add my testvar=testvalue to the list, because I tried so many files (although it seems none of them worked), by adding export testvar=testvalue to the following files:

  • /etc/paths
  • ~/.bashrc
  • ~/.bash_profile
  • ~/.profile
  • /etc/profile

Also after inserting it into each file I used source {file}.

So at this point I have no idea which is the proper way to create and have it permanently, and being able to use it in my Java code.

So far, I can print the variables into the terminal like this:

printenv

My variables are getting listed, example:

testvar=testvalue

In my Java code, I get null when using:

System.getenv("testvar")

However using an other variable names that were not created by me, but the macOS system (eg. "USER") prints the value as expected.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Jano
  • 401
  • 1
  • 5
  • 14
  • Aren't you supposed to do it with [export](https://mamk2118.medium.com/setting-up-environment-variables-in-macos-mojave-and-mac-os-catalina-27ea1bb032f3)? – Abra Jan 06 '21 at 16:07
  • there are "multiple ways" and multiple files you can insert it into, problem is, none of the worked, I tried them all. most of these infos are from stackoverflow posts that are 7 -10 -12 years old. So as in the main question, which file i should insert "export testvar=testvalue" to be able to use it in the java application with System.getenv("testvar") since even if my added variable shows in terminal's "printenv" I still get a null when trying to use it in the java code – Jano Jan 07 '21 at 07:35
  • https://stackoverflow.com/questions/135688/setting-environment-variables-on-os-x – Robert Sep 15 '21 at 19:41

3 Answers3

27

macOS Big Sur uses zsh as the default login shell and interactive shell.

If you’re using a Bash profile, such as to set environment variables, aliases, or path variables, you should switch to using a zsh equivalent.

For example:

  • .zprofile is equivalent to .bash_profile and runs at login, including over SSH
  • .zshrc is equivalent to .bashrc and runs for each new Terminal session

You can create .zprofile and enter the enter the environment variables there.


Reference

informatik01
  • 16,038
  • 10
  • 74
  • 104
Ankit Rai
  • 1,577
  • 1
  • 12
  • 24
  • Thanks for your answer! I will try to find some time to test it, and update here with the results. – Jano Mar 12 '21 at 08:02
  • I followed this but it doesn't work as my go app still returns an empty variable value after setting the variable first. What's odd is that `env` shows the variables I set. – dragonfly02 May 07 '21 at 06:33
  • 2
    I had success on Big Sur with permanent environment variables by creating `.zprofile` and adding entries like this inside `export [variable_name]=[variable_value]`. Also Helpful: [How to Set Environment Variables in MacOS](https://phoenixnap.com/kb/set-environment-variable-mac) – Michael R Dec 21 '21 at 00:19
  • 1
    The `.bash_profile`, `.bashrc`, `.zprofile` and `.zshrc` no longer seem to work in `Big Sur 11.6.5` but did on my system on `Big Sur 11.4` – Roy Hinkley Apr 12 '22 at 21:47
7

you can edit zprofile using the following command

sudo nano ~/.zprofile

and add your PATH variable.

# Setting PATH for Python 3.9
# The original version is saved in .zprofile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.9/bin:${PATH}"
export PATH

to add multiple values to the PATH variable, just add more PATH keys. For example, this is how I added multiple path variables in my M1 mac Monterey

# Setting PATH for Python 3.9
# The original version is saved in .zprofile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.9/bin:${PATH}"
PATH="/Users/<name>/.local/bin"
PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
export PATH
Dheeraj Inampudi
  • 1,227
  • 15
  • 11
1

This depends on the shell which you are using. For Big Sur, the standard shell is zsh, which might explain why .bashrc and other bash-related configuration files did not work. If you want to set environment variables for your account in zsh, try to create a ~/.zshenv file and put the variable declarations there.

See also: http://zsh.sourceforge.net/Doc/Release/Files.html#Files

Oliver Marienfeld
  • 1,154
  • 9
  • 17
  • Thanks! I will check it, and mark it as answer if I succeed. ( I will need a few days to be able to have time working on this again) – Jano Feb 09 '21 at 17:27