2

I have the following on my .vimrc

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

So the shell I am using is non-interactive and non-login. I thought that non-login shells source .bashrc, but that does not seem to be the case. I do not want to make my shell interactive or login. Is there a way for me source .bashrc in other way? My .bash_profile already sources .bashrc

3
  • Is there a reason you don't want an interactive shell? If you want to be able to launch commands and run things you will need an interactive shell. Also, are you sure about -c, in Linux GNU bash, -c is the command you want to run.
    – terdon
    Apr 21 '13 at 2:39
  • @terdon: interactive shell sets the current working directory to $HOME, which is not what I want. Yes, '-c` allows me to type the command in gvim command line (i.e. after :) so whatever I type in will become the command input to bash shell. Apr 21 '13 at 2:52
  • Related: vi.stackexchange.com/questions/11167/… Nov 12 '20 at 17:24
4

What you describe is normal behavior. Starting bash with the -c option will launch a non-interactive, non-login shell. This means that bash will not source any of its classic configuration files but the variable $BASH_ENV instead. As explained in the bash man page:

  • non-interactive, non-login shell:

When bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute. Bash behaves as if the following command were executed:

if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
  • interactive, login shell:

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.

  • interactive, non-login shell

When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of /etc/bash.bashrc and ~/.bashrc.


So, if you want your non-interactive, non-login shell to source ~/.bashrc, you will need to set the value of BASH_ENV to ~/.bashrc. Add this line to your ~/.bashrc or ~/.profile files:

export BASH_ENV=~/.bashrc
1
  • 1
    If .bashrc is not being read, then how will adding export BASH_ENV=~/.bashrc to .bashrc help?
    – Hashken
    Aug 10 '16 at 6:27
0

As already mentioned, -c causes the shell to be non-interactive. However, you can force the shell to be interactive by adding -i. This is described in :help :! (or here):

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.

The -c and -i options behave consistently in most shells, I think because they come from POSIX (see here):

-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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.