【问题标题】:MSYS2 does not source .profileMSYS2 不来源 .profile
【发布时间】:2014-06-07 16:18:21
【问题描述】:

使用 MSYS2,如果我运行 msys2_shell.batmintty 会打开一个 bash 登录 shell,但 ~/.profile 不会被获取。

无论如何,如果我在 mintty 中运行 /bin/bash --login~/.profile 就会得到资源。为什么?

如果我通过 Windows 提示符而不是 msys2_shell.bat 运行 path\to\msys64\bin\bash.exe --login,也会发生同样的情况。

PS:我也试过.bash_profile

【问题讨论】:

  • 在登录和不登录的情况下启动 shell 是有区别的。这就是为什么你需要 --login
  • 问题不在于登录 shell 是什么,而在于为什么 .profile 没有来源。
  • 因为你没有进入'login' shell.. 这是一个特性,也是'login' shell 和非'login' shell 之间的区别。
  • @xxcezz:你为什么说我不在登录shell? msys2_shell.bat 打开一个登录外壳。
  • 我误解了这个问题。对不起。您可以更新 msys2_shell.bat 以创建登录 shell 吗?

标签: mingw msys .bash-profile mintty msys2


【解决方案1】:

在系统范围内禁用(重命名)/etc/profile~/.profile 的来源。

在调查/etc/profile 之后,我看到了,保留它但评论函数profile_d () ~/.profile 是来源。该函数运行/etc/profile.d/ 中的脚本。

单独禁用它们我意识到罪魁祸首是/etc/profile.d/bash_completion.sh

上面写着:

# Check for interactive bash and that we haven't already been sourced.
[ -z "$BASH_VERSION" -o -z "$PS1" -o -n "$BASH_COMPLETION_COMPAT_DIR" ] && return

# Check for recent enough version of bash.
bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.}
if [ $bmajor -gt 4 ] || [ $bmajor -eq 4 -a $bminor -ge 1 ]; then
    [ -r "${XDG_CONFIG_HOME:-$HOME/.config}/bash_completion" ] && \
        . "${XDG_CONFIG_HOME:-$HOME/.config}/bash_completion"
    if shopt -q progcomp && [ -r /usr/share/bash-completion/bash_completion ]; then
        # Source completion code.
        . /usr/share/bash-completion/bash_completion
    fi
fi
unset bash bmajor bminor

第一行解释了为什么在运行子shell(第二次)时一切正常:环境变量已经设置,所以脚本返回。

问题是bash_completion.sh/usr/share/bash-completion/bash_completion,真的很大,很难把握问题。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。

    我在这张票的评论线程中得到了 Dan 的帮助:https://sourceforge.net/p/msys2/tickets/97

    解决办法是编辑/etc/fstab

    $ cat /etc/fstab
    # For a description of the file format, see the Users Guide
    # http://cygwin.com/cygwin-ug-net/using.html#mount-table
    
    # DO NOT REMOVE NEXT LINE. It remove cygdrive prefix from path
    none / cygdrive binary,posix=0,noacl,user 0 0
    
    d:/Users/dparker /home/dparker ntfs binary,posix=0,user 0 0
    

    注意最后一行是挂载你的主目录所必需的......我不确定你为什么需要在/etc/fstab 中明确地这样做,因为它似乎在没有它的情况下挂载它......但也许它不是t 安装它正确

    希望这对你有用,就像对我一样。

    【讨论】:

    • 如果路径中有空格,这似乎无法正常工作,许多 Windows 用户目录都会这样做。例如。 c:/Users/user\040name /home/user\040name ntfs ...。我最终得到了挂载目录中“ntfs”的一部分,即/home/user name nt