Mac OS X 中的环境变量 [英] Environment variables in Mac OS X

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

问题描述

更新:下面的链接没有完整的答案.必须在两个地方(一个用于 GUI,一个用于 shell)设置路径或变量是很蹩脚的.

不重复:在 OS X 中设置环境变量?

<小时>

在 Windows 背景下,设置和修改环境变量非常容易(只需转到系统属性">高级">环境变量"),在 Mac OS 10.5 上似乎并不那么简单.大多数参考资料都说我应该更新/etc/profile 或 ~/.profile.那些等同于系统变量和用户变量吗?例如,我应该在哪里设置我的 JAVA_HOME 变量?

<小时>

我希望能够从终端以及像 Eclipse 这样的应用程序访问变量.另外,我希望我不必重新启动/注销才能使其生效.

解决方案

无需重复.您可以使用 launchctl setenv 设置由 launchd(和子进程,即从 Spotlight 启动的任何程序)使用的环境变量.

例如,如果您想在 .bashrc 或其他地方设置后,在 launchd 中镜像当前路径:

PATH=whatever:you:wantlaunchctl setenv PATH $PATH

环境变量不会在运行的应用程序中自动更新.您需要重新启动应用程序以获取更新的环境变量(尽管您可以只在 shell 中设置变量,例如 PATH=whatever:you:want;无需重新启动终端).

Update: The link below does not have a complete answer. Having to set the path or variable in two places (one for GUI and one for shell) is lame.

Not Duplicate of: Setting environment variables in OS X?


Coming from a Windows background where it's very easy to set and modify environment variables (just go to System Properties > Advanced > Environment Variables), it does not seem to be that straight forward on Mac OS 10.5. Most references say I should update /etc/profile or ~/.profile. Are those the equivalent of System Variables and User Variables? For example, where should I set my JAVA_HOME variable?


EDIT:

I want to be able to access the variable from the terminal as well as an app like Eclipse. Also, I hope I don't have to restart/logout to make this take effect.

解决方案

There's no need for duplication. You can set environment variables used by launchd (and child processes, i.e. anything you start from Spotlight) using launchctl setenv.

For example, if you want to mirror your current path in launchd after setting it up in .bashrc or wherever:

PATH=whatever:you:want
launchctl setenv PATH $PATH

Environment variables are not automatically updated in running applications. You will need to relaunch applications to get the updated environment variables (although you can just set variables in your shell, e.g. PATH=whatever:you:want; there's no need to relaunch the terminal).

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

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