我已经阅读了几个关于如何在OSX上永久设置环境变量的答案。

首先,我尝试了这个,如何在Linux/Unix上永久设置$PATH,但我有一个错误消息说没有这样的文件和目录,所以我想我可以尝试~/。Bash_profile而不是~/。但它没有工作。

其次,我发现了这个解决方案如何设置的$PATH为使用的应用程序在os x,其中建议做改变

~/.MacOSX/environment.plist

但我没有这样的文件和目录错误。

我需要一种方法来设置这些变量,这样就不需要每次打开一个新的终端会话时都一次又一次地设置它们。


当前回答

我发现有一些文件可能会影响macOS中的$PATH变量(适用于我,10.11 El Capitan),列出如下:

As the top voted answer said, vi /etc/paths, which is recommended from my point of view. Also don't forget the /etc/paths.d directory, which contains files may affect the $PATH variable, set the git and mono-command path in my case. You can ls -l /etc/paths.d to list items and rm /etc/paths.d/path_you_dislike to remove items. If you're using a "bash" environment (the default Terminal.app, for example), you should check out ~/.bash_profile or ~/.bashrc. There may be not that file yet, but these two files have effects on the $PATH. If you're using a "zsh" environment (Oh-My-Zsh, for example), you should check out ~./zshrc instead of ~/.bash* thing.

不要忘记重新启动所有终端窗口,然后回显$PATH。$PATH字符串将是PATH_SET_IN_3&4:PATH_SET_IN_1:PATH_SET_IN_2。

注意,前两种方式(/etc/paths和/etc/path.d)在/目录下,这将影响您计算机中的所有帐户,而最后两种方式(~/. path.d)在/目录下。bash*或~/.zsh*)在~/目录下(也就是/Users/yourusername/),这只会影响你的帐户设置。

阅读更多:Mac OS X:设置/更改$PATH变量- nixCraft

其他回答

你必须把它添加到/etc/paths.

参考资料(对我有用):这里

显示所有隐藏文件,如.bash_profile和.zshrc $ ls -a

从macOS Catalina开始,mac使用zsh而不是bash。MAC默认使用zsh。 检查哪个shell正在运行:

$ echo $SHELL
/usr/zsh
$ cd $HOME
$ open -e .zshrc

或者如果使用vim

$ vi .zshrc

然后像这样加进去

$ export my_var="/path/where/it/exists"
$ export PATH=$PATH:/$my_var/bin

例如:如果在/Applications中安装了名为:myapp的应用程序 然后

export MYAPP_HOME=/Applications/myapp
export PATH=$PATH:$MYAPP_HOME/bin

或快捷方式

export PATH=${PATH}:/Applications/myapp/bin

祝你一生幸福!!稍后谢谢我

你还可以加上这个

if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

~ /。Bash_profile然后创建~/。bashrc,你可以添加更多的路径到PATH。一个例子。

export PATH=$PATH:.

我发现有一些文件可能会影响macOS中的$PATH变量(适用于我,10.11 El Capitan),列出如下:

As the top voted answer said, vi /etc/paths, which is recommended from my point of view. Also don't forget the /etc/paths.d directory, which contains files may affect the $PATH variable, set the git and mono-command path in my case. You can ls -l /etc/paths.d to list items and rm /etc/paths.d/path_you_dislike to remove items. If you're using a "bash" environment (the default Terminal.app, for example), you should check out ~/.bash_profile or ~/.bashrc. There may be not that file yet, but these two files have effects on the $PATH. If you're using a "zsh" environment (Oh-My-Zsh, for example), you should check out ~./zshrc instead of ~/.bash* thing.

不要忘记重新启动所有终端窗口,然后回显$PATH。$PATH字符串将是PATH_SET_IN_3&4:PATH_SET_IN_1:PATH_SET_IN_2。

注意,前两种方式(/etc/paths和/etc/path.d)在/目录下,这将影响您计算机中的所有帐户,而最后两种方式(~/. path.d)在/目录下。bash*或~/.zsh*)在~/目录下(也就是/Users/yourusername/),这只会影响你的帐户设置。

阅读更多:Mac OS X:设置/更改$PATH变量- nixCraft

sudo nano /etc/paths

现在找到命令路径,我给出了一个颤振路径设置的例子。

/Users/username/development/flutter/bin

现在ctrl +x,然后y。重新打开终端并检查。