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.
Environment variables are used to store system-wide values that can be used by any user and process under the operating system. Setting environment variables is essential in the steps of certain installations we covered such as How To Install Java or How To Install Java On Raspberry Pi.
Displaying current Environment Variables This is very easy. Just open the Terminal and run the command printenv as shown below. This will list all the environment variables currently set.
Displaying Current Environment & Shell Variables in Mac OS X. To quickly get a list of environmental variables, you can use the following command: printenv. If you want to see a complete list of shell variables, the ‘set’ command can be issued as well: set. The output of these commands can be lengthy so you may wish to pipe the output through ...
To set the value of an environment variable, use the appropriate shell command to associate a variable name with a value. For example, to set the variable PATH to the value /bin:/sbin:/user/bin:/user/sbin:/system/Library/, you would enter the following command in a Terminal window:
If you want the environment variable to be available to all users, you can change /etc/bashrc file. But I would prefer just changing one users' environment.
Find the path to .bash_profile by using: 2. Open the .bash_profile file with a text editor of your choice. 3. Scroll down to the end of the .bash_profile file. 4. Use the export command to add new environment variables: 5. Save any changes you made to the .bash_profile file. 6.
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.
Reference
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With