lallina
lallina

Reputation: 33

Setting environment variables on macOS Catalina

In which file should I set environment variables on macOS Catalina?

I have already tried the .bash_profile file but it does't work.

Upvotes: 1

Views: 3005

Answers (4)

Kristian Heitkamp
Kristian Heitkamp

Reputation: 779

macOS Catalina and later macOSs do not use the bash shell as default anymore. Instead zsh is used.

Go to your user's home directory. In the shell you type cd ~/.zprofile, if you prefer a GUI you go to the Finder, then to the home icon and your user name. There you press command shift . to view hidden files and folders. Open the file .zprofile either using TextEdit or another text editor.

Add a line in the following format:

export VARIABLES_NAME=value

Don't use quotes around the value even if it is a string. Save the file, open a new terminal, and type echo $VARIABLE_NAME to confirm the variable's persistance.

Upvotes: 0

Mba Gozpel
Mba Gozpel

Reputation: 213

For Zsh shell, type this in your terminal:

$ echo 'export ENV_VAR=value' >> ~/.zshenv

Then restart your terminal and type printenv to confirm if it's there.

To view the value of your env_var type the following:

$ echo $ENV_VAR

Upvotes: 2

lallina
lallina

Reputation: 33

I've set in zprofile and it works. Thanks!

Upvotes: 0

ch271828n
ch271828n

Reputation: 17653

Firstly, you can set it in your ~/.bashrc

Secondly, you need to restart your shell, or run source ~/.bashrc to activate it.

Thirdly, make sure you are using bash. e.g. if using zsh then you need ~/.zshrc.

Lastly, ensure your code in the file is correct. e.g. export PATH="$PATH:/Users/tom/.cargo/bin" as a line in ~/.bashrc.

Upvotes: 0

Related Questions