Reputation: 511
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.
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:
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.
Upvotes: 32
Views: 62279
Reputation: 1457
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
Upvotes: 12
Reputation: 1717
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 sessionYou can create .zprofile
and enter the enter the environment variables there.
Upvotes: 34
Reputation: 1393
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
Upvotes: 2