bash 在 OSX 中永久设置 PATH 环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22465332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:43:56  来源:igfitidea点击:

Setting PATH environment variable in OSX permanently

macosbashunixpathenvironment-variables

提问by patti_jane

I have read several answers on how to set environmental variables on OSX as permanently.

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

First, I tried this, How to permanently set $PATH on Linux/Unix?but I had an error message saying no such file and directory, so I thought I could try ~/.bash_profileinstead of ~/.profilebut it did not work.

首先,我试过这个,如何在 Linux/Unix 上永久设置 $PATH?但是我有一条错误消息说no such file and directory,所以我想我可以尝试 ~/.bash_profile代替,~/.profile但它没有用。

Second, I found this solution How to set the $PATH as used by applications in os x, which advices to make changes in

其次,我找到了这个解决方案How to set the $PATH as used by applications in os x,它建议在

~/.MacOSX/environment.plist

~/.MacOSX/environment.plist

but again I had no such file and directoryerror.

但我又no such file and directory犯了错误。

I need a way to set these variables such that it won't require to set them again and again each time I open a new terminal session.

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

回答by Nitish

You have to add it to /etc/paths.

您必须将其添加到/etc/paths.

Reference (which works for me) : Here

参考(对我有用):这里

回答by iplus26

I've found that there are some files that may affect the $PATHvariable in macOS (works for me, 10.11 El Capitan), listed below:

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

  1. As the top voted answer said, vi /etc/paths, which is recommended from my point of view.

  2. Also don't forget the /etc/paths.ddirectory, which contains files may affect the $PATHvariable, set the gitand mono-commandpath in my case. You can ls -l /etc/paths.dto list items and rm /etc/paths.d/path_you_disliketo remove items.

  3. If you're using a "bash" environment (the default Terminal.app, for example), you should check out ~/.bash_profileor ~/.bashrc. There may be not that file yet, but these two files have effects on the $PATH.

  4. If you're using a "zsh" environment (Oh-My-Zsh, for example), you should check out ~./zshrcinstead of ~/.bash*thing.

  1. 正如投票最高的答案所说,vi /etc/paths从我的角度推荐。

  2. 另外不要忘记/etc/paths.d包含可能影响$PATH变量的文件的目录,在我的情况下设置gitmono-command路径。您可以ls -l /etc/paths.d列出项目和rm /etc/paths.d/path_you_dislike删除项目。

  3. 如果您使用的是“bash”环境(Terminal.app例如,默认的),您应该查看~/.bash_profile~/.bashrc。可能还没有那个文件,但这两个文件对$PATH.

  4. 如果您使用的是“zsh”环境(例如Oh-My-Zsh),则应该检查~./zshrc而不是~/.bash*事物。

And don't forget to restart all the terminal windows, then echo $PATH. The $PATHstring will be PATH_SET_IN_3&4:PATH_SET_IN_1:PATH_SET_IN_2.

并且不要忘记重新启动所有终端窗口,然后echo $PATH. 该$PATH字符串会PATH_SET_IN_3&4:PATH_SET_IN_1:PATH_SET_IN_2

Noticed that the first two ways (/etc/pathsand /etc/path.d) is in /directory which will affect all the accounts in your computer while the last two ways (~/.bash*or ~/.zsh*) is in ~/directory (aka, /Users/yourusername/) which will only affect your account settings.

请注意,前两种方式 (/etc/paths/etc/path.d) 位于/目录中,会影响您计算机中的所有帐户,而后两种方式 (~/.bash*~/.zsh*) 位于~/目录中 ( 又名/Users/yourusername/) ,只会影响您的帐户设置。

Read more: Mac OS X: Set / Change $PATH Variable - nixCraft

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

回答by omoman

You could also add this

你也可以添加这个

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

to ~/.bash_profile, then create ~/.bashrcwhere you can just add more paths to PATH. An example with .

to ~/.bash_profile,然后创建~/.bashrc可以向 PATH 添加更多路径的位置。一个例子.

export PATH=$PATH:.

回答by Tiago

You can open any of the following files:

您可以打开以下任何文件:

/etc/profile
~/.bash_profile
~/.bash_login   (if .bash_profile does not exist)
~/.profile      (if .bash_login does not exist)

And add:

并添加:

export PATH="$PATH:your/new/path/here"

回答by Rishabh Mishra

For a new path to be added to PATH environment variable in MacOS just create a new file under /etc/paths.ddirectory and add write path to be set in the file. Restart the terminal. You can check with echo $PATHat the prompt to confirm if the path was added to the environment variable.

在 MacOS 中要添加到 PATH 环境变量的新路径只需在/etc/paths.d目录下创建一个新文件并添加要在文件中设置的写入路径。重启终端。您可以echo $PATH在提示符处检查以确认路径是否已添加到环境变量中。

For example: to add a new path /usr/local/sbinto the PATHvariable:

例如:添加一个新的路径/usr/local/sbinPATH变量:

cd /etc/paths.d
sudo vi newfile

Add the path to the newfileand save it.

添加路径newfile并保存。

Restart the terminal and type echo $PATHto confirm

重启终端并输入echo $PATH确认

回答by IgnitedMind

i tried the first method and gone through reference page,well executed path setting.But it did not shoe the path set when i echo$PATH

我尝试了第一种方法并浏览了参考页面,路径设置执行得很好。但是当我 echo$PATH 时它没有设置路径

回答by Ram Krishna

For setting up path in Mac two methods can be followed.

在 Mac 中设置路径有两种方法。

  1. Creating a file for variable name and paste the path there under /etc/paths.d and source the file to profile_bashrc.
  2. Export path variable in ~/.profile_bashrcas

    export VARIABLE_NAME = $(PATH_VALUE)

  1. 为变量名创建一个文件并将路径粘贴到 /etc/paths.d 下,并将文件来源到 profile_bashrc。
  2. 将路径变量导出~/.profile_bashrc

    导出变量名 = $(PATH_VALUE)

AND source the the path. Its simple and stable.

并提供路径。它简单而稳定。

You can set any path variableby Mac terminalor in linuxalso.

您也可以设置任何path variablebyMac terminal或 in linux