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
(add-to-list 'load-path "C:/Program Files/emacs-site-lisp/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?

2 Answers

The best answer I can give is a link that explains the differences between .bash_profile and .bashrc:

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.

1

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like