I am using mac os mojave (10.14.3). I set the environment variable in both ~/.bash_profile
and ~/.bashrc
and I ran both ~/.bash_profile
and ~/.bashrc
. Then in the same terminal I can see the values which I set (using printenv
), but if I open a new terminal then I can not see previously set env variable.
Please give some suggestions.
In mac os Bigsur(11.2.3), the default terminal was zsh ( I did not check for other versions) and setting envs in ~/.zshenv
as in
echo 'export PATH=$PATH:$PATH:~/Library/Android/sdk/build-tools/29.0/' >> ~/.zshenv
and running . ~/.zshenv
. ~/.zshenv
helped me saving the envs permanently
This works for OS X 10.14 "Mojave":
Step 1: go to your $HOME/Library/LaunchAgents
directory and create setenv.MY_VAR.plist
file with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>setenv.MY_VAR</string>
<key>ProgramArguments</key>
<array>
<string>/bin/launchctl</string>
<string>setenv</string>
<string>MY_VAR</string>
<string>SOME_VALUE_FOR_MY_VAR</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
Pay attantion that your file name and entries in the file match.
STEP 2: Run launchctl load ~/Library/LaunchAgents/setenv.MY_VAR.plist
or - restart the system.
STEP 3: Restart your Terminal app.
STEP 4: Check if the var is there: env
. It should give you: MY_VAR=SOME_VALUE_FOR_MY_VAR
.
If you want to do more changes, first do launchctl unload...
than launchctl load...
again.
This is per user setting. If you want to set for all users, try doing the same in /Library/LaunchAgents
.
Not sure how you're setting the environment variables, but make sure that you're using the export command to persist it across shells, eg export EDITOR=/usr/bin/vim
. You can then check what variables were exported from another shell with export -p
.
Running bash
directly from the command line will source your.bashrc file. If the.bashrc file isn't being sourced when you open a new terminal window, it's possible that you're not running bash .
In the case where /bin/sh
is by default your default shell (as opposed to bash), you can change that by running chsh -s $(which bash)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.