gVim 不会从非交互式非登录 shell 中获取 .bashrc、.bash_profile 或 .profile

gVim 不会从非交互式非登录 shell 中获取 .bashrc、.bash_profile 或 .profile

我的 .vimrc 上有以下内容

set shell=C:/cygwin/bin/bash
set shellcmdflag=-c
set shellxquote=\"

所以我使用的 shell 是非交互式和非登录的。我以为非登录 shell 会获取 .bashrc,但似乎并非如此。我不想制作我的 shellinteractivelogin。我有没有其他方法可以获取 .bashrc?我的 .bash_profile 已经获取 .bashrc

答案1

您所描述的是正常行为。使用该-c选项启动 bash 将启动非交互式、非登录shell。这意味着 bash 将不会获取其任何经典配置文件,而是获取变量$BASH_ENV。如 bash 手册页中所述:

  • 非交互式、非登录 shell:

当 bash 以非交互方式启动时,例如要运行 shell 脚本,它会查找变量BASH_ENV在环境中,如果出现该命令,则扩展其值,并使用扩展的值作为要读取和执行的文件的名称。 Bash 的行为就像执行了以下命令一样:

if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
  • 交互式登录 shell:

当 bash 作为交互式登录 shell 或使用 --login 选项作为非交互式 shell 调用时,它首先从文件读取并执行命令/etc/配置文件,如果该文件存在。读取该文件后,它会查找 的〜/ .bash_profile〜/ .bash_login, 和〜/ .profile,按该顺序,并从第一个存在且可读的命令中读取并执行命令。

  • 交互式、非登录 shell

当启动非登录 shell 的交互式 shell 时,bash 会从 /etc/bash.bashrc~/.bashrc,如果这些文件存在。这可以通过使用 --norc选项。 --rc文件 文件选项将强制 bash 从文件而不是 /etc/bash.bashrc 和 ~/.bashrc 读取并执行命令。


因此,如果您希望非交互式、非登录 shell 能够 source ~/.bashrc,则需要将 的值设置BASH_ENV~/.bashrc。将此行添加到您的~/.bashrc~/.profile文件中:

export BASH_ENV=~/.bashrc

答案2

如上所述,-c会导致 shell 为非交互式。但是,您可以通过添加 来强制 shell 为交互式-i。这在:help :!(或这里):

On Unix the command normally runs in a non-interactive
shell.  If you want an interactive shell to be used
(to use aliases) set 'shellcmdflag' to "-ic".
For Win32 also see :!start.

-c和选项-i在大多数 shell 中表现一致,我想是因为它们来自 POSIX(参见这里):

-c
    Read commands from the command_string operand. Set the value of special parameter 0
    (see Special Parameters) from the value of the command_name operand and the positional
    parameters ($1, $2, and so on) in sequence from the remaining argument operands. No
    commands shall be read from the standard input.
-i
    Specify that the shell is interactive; see below. An implementation may treat
    specifying the -i option as an error if the real user ID of the calling process does
    not equal the effective user ID or if the real group ID does not equal the effective
    group ID.

相关内容