# Getting GNU Emacs Windows to run my ~/.bash_profile when creating a Cygwin M-x shell subprocess

I use GNU Emacs on Windows and Cygwin for the shell that I do all my work in. The ~/.profile used by Cygwin is ~/.bash_profile. If I start a Cygwin shell by running C:\cygwin\Cygwin.bat, it starts up and executes ~/.bash_profile fine.

I set up Cygwin in GNU Emacs on Windows by putting the following in my ~/.emacs, as prescribed:

;; Set up Cygwin
(require 'setup-cygwin)


C:\Program Files\emacs-site-lisp is where I put my site-wide lisp (obviously).

When I run M-x shell, a Cygwin bash subprocess is created in a buffer named *shell*. Perfect. However, my ~/.bash_profile is not executed.

How do I make the ~/.bash_profile take effect?

-

The best answer I can give is a link that explains the differences between .bash_profile and .bashrc: http://joshstaiger.org/archives/2005/07/bash_profile_vs.html

There's a whole lot of history there and some of it has to do with The Old Days when we'd dial in to systems with slow modems and we wanted a shell up and running fast. So we'd want to "login" with an environment that was quick to setup and then spawn "sub-shells" that were fully configured because that was faster to do from an already logged in shell.

In your case your inital shell is being started as a login shell (check cygwin.bat -- it's calling bash with --login). But the emacs sub-shell is not being started as a login shell. So only your .bashrc file is being loaded.

These days I think most people just keep it all in .bashrc and have .bash_profile load that instead:

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


But you need to be careful that you don't repeatedly append to variables like PATH if you take this approach. Otherwise, every sub-shell ends up with duplicate entries on the PATH and you can get really long and unruly variables that need to be searched and things slow down.

-
The distinction between .profile and .bashrc has nothing to do with efficiency: they're for different purposes. ~/.profile or ~/.bash_profile is for stuff to do on login, such as setting environment variables and running programs that you want to start once per session. ~/.bashrc is for configuring the interactive shell (aliases, key bindings, etc). The reason why you'd want to source ~/.bashrc from ~/.bash_profile is a design bug in bash: it doesn't read ~/.bashrc in a login shell, even if that login shell is interactive. – Gilles Sep 14 '10 at 18:21

This is how I do it - and it works:

(setq explicit-bash-args '("--login" "--init-file" "c:/home/cbalz/.bash_profile" "-i"))


One would think that 'bash.exe' would run '.bash_profile' without the explicit init file, since this is a login shell ('--login') and 'bash.exe' does properly find and execute '.bashrc'. This is not the case however. Since '.bash_profile' sources '.bashrc', the solution is to just run '.bash_profile'.

I think Cygwin should run a Windows service to manage the environment variables, so that shells could inherit properly.

-