【问题标题】:Git for Windows doesn't execute my .bashrc file适用于 Windows 的 Git 不执行我的 .bashrc 文件
【发布时间】:2015-11-18 03:28:12
【问题描述】:

我刚刚在 Windows 7 上安装了适用于 Windows 2.5.0 的 Git,当我运行 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 运行。
  • 不,我什至没有.profile.bash_profile。但是您使用的是适用于 Windows 2.5.0 的 Git 吗?

标签: windows git bash windows-7


【解决方案1】:

好的,我发现了问题。很简单,最新的 Git for Windows 2.5.0 (mintty) 使用的 bash 终端不需要阅读 .bashrc - 它会阅读 .bash_profile。因此,您可以在.bash_profile 中设置您的环境和/或将此代码放在开头以阅读.bashrc

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

【讨论】:

  • 这不是薄荷本身,而是 git-bash.exe 如何启动它。在这里进行了长时间的讨论,基本上以更改安装程序以创建 .bash_profile 为结束,就像您描述的那样:github.com/git-for-windows/git/issues/191
  • 解决方案今天 2020 年 5 月在 Windows 10 git bash 上仍然有效
【解决方案2】:

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

mv ~/.bashrc ~/.bash_profile

【讨论】:

  • 这与上面的几乎相同,但它们都很好用
  • 小心不要覆盖你的 conda .bash_profile
【解决方案3】:

似乎最新版本的 git for Windows (2.8.3.windows.1) 现在使用“配置文件”文件而不是 .bash_profile。我认为这是因为它不是隐藏的并且是有效的文件名。以前似乎没有引起任何问题,但可能会让人感到困惑。

【讨论】:

    【解决方案4】:

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

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

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

    【讨论】:

    • mintty 没有参数 -rcfile,这就是为什么 mintty 需要 .bash_profile
    猜你喜欢
    • 2011-10-16
    • 2012-08-08
    • 1970-01-01
    • 2017-03-09
    • 2014-04-11
    • 1970-01-01
    • 2021-05-03
    • 2013-02-12
    • 1970-01-01
    相关资源
    最近更新 更多