【问题标题】:.bashrc not executed when starting GitBash启动 GitBash 时未执行 .bashrc
【发布时间】:2021-06-05 23:10:57
【问题描述】:

我正在关注this guide,我已经到了需要创建 .bashrc 文件的部分,当我重新启动 GitBash 时它似乎没有执行。我已经尝试了一些东西,包括this SO question 上的答案。我也尝试过在其中添加一个 echo 进行测试,这在我直接执行时有效,但在我启动 GitBash 时无效。

【问题讨论】:

  • 你想在bashrc中执行什么功能?
  • 我参考的指南中的那个。

标签: git bash ssh git-bash


【解决方案1】:

我通过执行git-bash -l -c bash 解决了这个问题(-l 是小写的 L)。 这样我的 $HOME/.bashrc 就会立即执行。
我在 Windows 7 上使用 PortableGit 2.5.0。

【讨论】:

  • 我会试试这个,但我修复它的方法是使用 .bash_profile。
  • 是的,同样的事情也对我有用。显然最近发生了一些变化,所以只有 ~/.bash_profile 是来源,而不是 ~/.bashrc。
【解决方案2】:

从 Git 2.5.0 开始,.bashrc 似乎不再被引用到 %USERPROFILE%\.bashrc。

但是你可以编辑这个文件:“C:\Program Files\Git\etc\bash.bashrc”。这是一个全局文件,但它必须这样做。

另外我认为建议编辑位于“C:\Program Files\Git\etc\profile.d”的文件,而不是创建 .bashrc。

该文件夹中的文件如下所示:

一些好的标准,如果用户不使用的话

创建他/她自己的 .bashrc/.bash_profile

【讨论】:

    【解决方案3】:

    ~/.bashrc 不是默认来源,但~/.profile 是...

    您只需创建一个 ~/.profile 来获取您的 ~/.bashrc

    下面是我的 cygwin 附带的 ~/.profile,我复制到我的 git-bash 的 ~ 中。

    # To the extent possible under law, the author(s) have dedicated all
    # copyright and related and neighboring rights to this software to the
    # public domain worldwide. This software is distributed without any warranty.
    # You should have received a copy of the CC0 Public Domain Dedication along
    # with this software.
    # If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
    
    # base-files version 4.2-4
    
    # ~/.profile: executed by the command interpreter for login shells.
    
    # The latest version as installed by the Cygwin Setup program can
    # always be found at /etc/defaults/etc/skel/.profile
    
    # Modifying /etc/skel/.profile directly will prevent
    # setup from updating it.
    
    # The copy in your home directory (~/.profile) is yours, please
    # feel free to customise it to create a shell
    # environment to your liking.  If you feel a change
    # would be benificial to all, please feel free to send
    # a patch to the cygwin mailing list.
    
    # User dependent .profile file
    
    # Set user-defined locale
    export LANG=$(locale -uU)
    
    # This file is not read by bash(1) if ~/.bash_profile or ~/.bash_login
    # exists.
    #
    # if running bash
    if [ -n "${BASH_VERSION}" ]; then
      if [ -f "${HOME}/.bashrc" ]; then
        source "${HOME}/.bashrc"
      fi
    fi
    

    【讨论】:

      【解决方案4】:

      首先,确保使用最新的 Git:

      • 解压 PortableGit-2.5.0-64-bit.7z.exe 到任何你想要的地方,并将 C:\path\to\PortableGit-2.5.0-64-bit\bin 添加到你的%PATH%
      • 然后更新您的%USERPROFILE%\.bashrc
      • 最后,启动C:\path\to\PortableGit-2.5.0-64-bit\git-bash.exe

      如果您在修改 .bashrc 时已经处于 bash 会话中,请再次获取它以查看它是否确实执行了您需要的操作。

      source .bashrc
      

      【讨论】:

      • 它是新安装的。
      • @Zachrip 是通过 msi 设置安装的,还是通过简单的未压缩存档(如可自动解压的 PortableGit-2.5.0-64-bit.7z.exe)安装的?
      【解决方案5】:

      Win10 上带有 mingw64 的默认 git-bash 似乎不是 .bash_rc.profile 的来源。

      请改用.bash_profile

      最初的问题(以及通过提供的链接提供的网站)谈论ssh-agent。在 git-bash 退出时通过 echo 'kill -KILL $SSH_AGENT_PID' &gt;&gt; .bash_logout 清理代理是个好主意。 git-bash 似乎让这些代理跨会话运行,但由于变量已经消失,因此无法再访问它们。 .bash_logout 来源于 git-bash

      在Win10机器上测试$git version给出git version 2.20.1.windows.1

      【讨论】:

        猜你喜欢
        • 2012-08-08
        • 2022-10-06
        • 2014-09-21
        • 1970-01-01
        • 1970-01-01
        • 2022-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多