Mac OS X Mojave-永久设置环境变量 [英] Mac OS X Mojave - set environment variable permanently

查看:194
本文介绍了Mac OS X Mojave-永久设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用macos mojave(10.14.3).我在两者中都设置了环境变量 ~/.bash_profile ~/.bashrc ,我同时运行了~/.bash_profile~/.bashrc.然后,在同一终端中,我可以看到我设置的值(使用printenv),但是如果我打开一个新终端,则看不到先前设置的env变量.

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.

请提出一些建议.

推荐答案

此方法适用于OS X 10.14"Mojave":

This works for OS X 10.14 "Mojave":

步骤1:转到您的$HOME/Library/LaunchAgents目录,并创建具有以下内容的setenv.MY_VAR.plist文件:

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>

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

第2步:运行launchctl load ~/Library/LaunchAgents/setenv.MY_VAR.plist或-重新启动系统.

STEP 2: Run launchctl load ~/Library/LaunchAgents/setenv.MY_VAR.plist or - restart the system.

第3步:重新启动终端应用程序.

STEP 3: Restart your Terminal app.

步骤4:检查var是否存在:env.它应该给您:MY_VAR=SOME_VALUE_FOR_MY_VAR.

STEP 4: Check if the var is there: env. It should give you: MY_VAR=SOME_VALUE_FOR_MY_VAR.

如果要进行更多更改,请先执行launchctl unload...而不是launchctl load....

If you want to do more changes, first do launchctl unload... than launchctl load... again.

这是每个用户设置.如果要为所有用户设置,请尝试在/Library/LaunchDaemons中进行相同的操作.

This is per user setting. If you want to set for all users, try doing the same in /Library/LaunchDaemons.

这篇关于Mac OS X Mojave-永久设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆