Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How exactly to set up and use environment variables on mac

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.

like image 772
Jano Avatar asked Jan 06 '21 14:01

Jano


People also ask

Do we need to set environment variables in Mac?

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.

Where are my environment variables Mac?

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.

How to display current environment and shell variables in Mac OS X?

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 ...

How to set the value of an environment variable in Linux?

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:

How to make environment variables available to all users?

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.

How to add environment variables to a bash_profile?

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.


Video Answer


3 Answers

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

like image 123
Ankit Rai Avatar answered Oct 21 '22 09:10

Ankit Rai


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

like image 22
rowing-ghoul Avatar answered Oct 21 '22 08:10

rowing-ghoul


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
like image 23
Dheeraj Avatar answered Oct 21 '22 10:10

Dheeraj