Git for Windows不能执行我的.bashrc文件

60

我刚刚在Windows 7上安装了Git for Windows 2.5.0,但是当我运行Git Bash时,我的.bashrc文件似乎没有被执行。

我是这样创建这个文件的:

Administrator@HintTech-Dev MINGW64 /
$ pwd
/

Administrator@HintTech-Dev MINGW64 /
$ cd ~

Administrator@HintTech-Dev MINGW64 ~
$ pwd
/c/Users/Administrator

Administrator@HintTech-Dev MINGW64 ~
$ touch .bashrc

Administrator@HintTech-Dev MINGW64 ~
$ vi .bashrc

[... I insert the line "ZZZTESTVAR=234" (without the quotes) into the file in vim ...]

Administrator@HintTech-Dev MINGW64 ~
$ exit

然而,当我下次运行Git Bash时:

Administrator@HintTech-Dev MINGW64 /
$ set | grep ZZZ

Administrator@HintTech-Dev MINGW64 /
$ cat ~/.bashrc
ZZZTESTVAR=234

Administrator@HintTech-Dev MINGW64 /
$ ZZZTESTVAR=234

Administrator@HintTech-Dev MINGW64 /
$ set | grep ZZZ
ZZZTESTVAR=234

Administrator@HintTech-Dev MINGW64 /
$

为什么我的.bashrc没有被执行?它似乎在正确的位置并且有正确的权限。


我正在运行 Git bash 版本 1.9.5-preview20150319,并运行了与您相同的测试,但对我来说,它起作用了。我的 .bashrc 已经运行。变量已经设置。你有 .profile.bash_profile 阻止了吗?这些会阻止运行 .bashrc - Wolf
没有,我甚至没有.profile.bash_profile文件。但是你用的是Git for Windows 2.5.0吗? - Jez
4个回答

103

好的,我找到了问题所在。简单来说,最新的Git for Windows 2.5.0使用的bash终端(mintty)不会读取.bashrc - 它会读取.bash_profile。因此,您可以在.bash_profile中设置您的环境变量,并/或者在开头放置以下代码以读取.bashrc

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

1
这并不是关于mintty本身的问题,而是git-bash.exe启动它的方式。在此有一篇长期的讨论,基本上是通过更改安装程序来创建像你所描述的.bash_profile文件:https://github.com/git-for-windows/git/issues/191 - Marc Stober
2
解决方案至今(2020年5月)仍可在Windows 10 Git Bash上运行。 - bsheps

29

当我在Windows 10中升级到Git Bash 2.5.0时,我遇到了同样的问题。我将我的 '.bashrc' 文件重命名为 '.bash_profile' 并重新启动了Git Bash。现在一切都像往常一样正常工作了。

mv ~/.bashrc ~/.bash_profile

1
这几乎与上面的那个完全相同,但它们都很有效。 - Mike_K
小心不要覆盖你的conda .bash_profile文件。 - Ismael EL ATIFI
截至2022年10月,一旦您创建了.bashrc文件,重新打开终端后 .bash_profile将自动创建。 - Elated Coder

3
看起来 Windows 版本的 Git(2.8.3.windows.1)现在使用“profile”文件替代 .bash_profile 了。我猜这是为了让它不再隐藏且具有有效的文件名。之前似乎没有造成任何问题,但也许对某些人来说比较困惑。

1

也许有点晚了,但是你可以使用-rcfile参数调用bash,然后跟上你的.bashrc文件的位置。

bash -rcfile C:\Users\name\.bashrc

我已将此添加到我的PowerShell配置文件(.ps1文件)的末尾,以便Powershell自动在bash中打开并按照我的偏好进行配置。


mintty没有-param -rcfile参数,这就是为什么需要使用.minttyrc文件的原因。 - Gunnar
这个命令适用于Windows系统上的终端应用程序:"C:\Program Files\Git\bin\bash.exe" -rcfile 'UserPathToYourEterntity/.bashrc' - gawkface

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接