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

Git for Windows doesn't execute my .bashrc file

我刚刚在 Windows 7 上为 Windows 2.5.0 安装了 Git,当我 运行 Git Bash.

我是这样创建文件的:

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 用于 Windows 2.5.0 (mintty) 的 bash 终端不会去读 .bashrc - 它读 .bash_profile .所以你可以在 .bash_profile and/or 中设置你的环境把这段代码放在开始阅读 .bashrc:

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

我在 Windows 10 中升级到 Git Bash 2.5.0 时发生了同样的事情。我重命名了我的 '.bashrc' -> '.bash_profile' 并重新启动 Git Bash。一切又恢复正常了。

mv ~/.bashrc ~/.bash_profile

Windows 的最新版本 git (2.8.3.windows.1) 现在使用 'profile' 文件而不是 .bash_profile.我假设这是因为它没有被隐藏并且是一个有效的文件名。之前似乎没有引起任何问题,但可能让人感到困惑。

这个答案可能有点晚了,但是您可以使用 -rcfile 参数调用 bash,后跟 .bashrc 文件的位置。

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

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