在OS X中修改环境变量如PATH的正确方法是什么?
我看了谷歌一点,找到了三个不同的文件进行编辑:
/etc/paths ~ / . profile ~ / tcshrc
我甚至没有这些文件中的一些,我很确定.tcshrc是错误的,因为OS X现在使用bash。这些变量,特别是PATH,定义在哪里?
我运行的是OS X v10.5 (Leopard)。
在OS X中修改环境变量如PATH的正确方法是什么?
我看了谷歌一点,找到了三个不同的文件进行编辑:
/etc/paths ~ / . profile ~ / tcshrc
我甚至没有这些文件中的一些,我很确定.tcshrc是错误的,因为OS X现在使用bash。这些变量,特别是PATH,定义在哪里?
我运行的是OS X v10.5 (Leopard)。
当前回答
嗯,我不确定/etc/paths和~/. macosx /environment。plist文件。那些是新的。
但是使用Bash时,您应该知道每次新的shell调用都会执行.bashrc 并且.bash_profile只在启动时执行一次。
我不知道在Mac OS x上这种情况发生的频率有多高。我想随着windows系统启动一切,这种区别已经消失了。
就我个人而言,我通过创建一个.bashrc文件来消除困惑,其中包含我需要的所有内容,然后执行:
ln -s .bashrc .bash_profile
其他回答
更新(2017-08-04)
从macOS 10.12.6 (Sierra)开始,这个方法似乎已经停止在Apache httpd上工作(对于系统和launchctl配置的用户选项)。其他项目似乎没有受到影响。可以想象,这是httpd中的一个bug。
原来的答案
这涉及到OS X 10.10+(特别是10.11+由于无根模式,其中/usr/bin不再可写)。
我在很多地方读到过,使用launchctl setenv PATH <new PATH >来设置PATH变量由于OS X中的一个错误而不起作用(从个人经验来看似乎是正确的)。我发现还有另一种方法可以为非shell启动的应用程序设置PATH:
sudo launchctl config user path <new path>
这个选项记录在launchctl手册页中:
config system | user parameter value Sets persistent configuration information for launchd(8) domains. Only the system domain and user domains may be configured. The location of the persistent storage is an implementation detail, and changes to that storage should only be made through this subcommand. A reboot is required for changes made through this subcommand to take effect. [...] path Sets the PATH environment variable for all services within the target domain to the string value. The string value should conform to the format outlined for the PATH environment variable in environ(7). Note that if a service specifies its own PATH, the service-specific environment variable will take precedence. NOTE: This facility cannot be used to set general environment variables for all services within the domain. It is intentionally scoped to the PATH environment vari- able and nothing else for security reasons.
我已经确认这与从Finder(使用getenv获取PATH)开始的GUI应用程序一起工作。 请注意,您只需要这样做一次,并且更改将在重新启动时保持不变。
直到和包括OS X v10.7 (Lion),你可以设置它们:
~/.MacOSX/environment.plist
See:
https://developer.apple.com/legacy/library/qa/qa1067/_index.html https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPRuntimeConfig/Articles/EnvironmentVars.html
对于终端中的PATH,您应该能够在.bash_profile或.profile中设置(尽管您可能必须创建它)
对于OS X v10.8 (Mountain Lion)及以上版本,您需要使用launchd和launchctl。
$PATH变量也受制于path_helper,它反过来利用/etc/paths文件和/etc/paths.d中的文件
更详细的描述可以在PATH和Leopard(2008-11)的其他环境问题中找到。
我的个人实践是.bash_profile。我在这里添加路径并附加到Path变量,
GOPATH=/usr/local/go/bin/
MYSQLPATH=/usr/local/opt/mysql@5.6/bin
PATH=$PATH:$GOPATH:$MYSQLPATH
之后,我可以有个人的路径通过echo$ GOPATH, echo$MYSQLPATH或所有通过echo$ Path。
这里有两种类型的炮弹。
非登录:每次启动一个新的Bash副本时,.bashrc都会被重新加载 登录:.profile只有在登录或显式告诉Bash加载它并将其用作登录shell时才会被加载。
在这里,重要的是要理解在Bash中,.bashrc文件只能由交互式和非登录的shell读取,您会发现人们经常在.bash_profile中加载.bashrc以克服这一限制。
现在您已经有了基本的了解,让我们继续讨论我建议您如何设置它。
.profile:创建一个不存在的文件。把你的PATH设置放在那里。 .bashrc:如果不存在则创建。把你所有的别名和自定义方法都放进去。 .bash_profile:如果不存在则创建。把下面的东西放进去。
.bash_file:
#!/bin/bash
source ~/.profile # Get the PATH settings
source ~/.bashrc # Get Aliases and Functions
#