polyglot
polyglot

Reputation: 10155

How to make emacs shell execute init file automatically?

My bash init script (~/.profile) contains important initializations, but whenever I use shell-command or compile in emacs, that init file is not read, and without those initialization my bash commands in emacs will fail :( How do I force emacs to execute my init file for the shell that it's using for shell-command?

Clarification: I'm not talking about M-x shell, which reads my init file just fine when I symlinked .profile to .emacs_bash.

Upvotes: 13

Views: 8770

Answers (3)

Adam Rosenfield
Adam Rosenfield

Reputation: 400146

Move your initialization routines into your ~/.bashrc file

Then add the line

source ~/.bashrc

into your ~/.bash_profile file. See the bash man page for a detailed explanation of which startup files get loaded when. The key is the difference between an interactive and non-interactive shell, as well as the difference between a login and a non-login shell.

In general, .bash_profile is run when logging into the system using a shell while .bashrc is run when starting a new shell (The OS X Terminal application is an exception to this rule as it runs .bash_profile for each new terminal instance). For more info in article form, look here.

Upvotes: 22

Imskull
Imskull

Reputation: 1315

I'm using Mac, shell-command did not execute .bashrc, nor .bash_profile, I googled a solution to have my PATH problem fixed.

put this in your .emacs: (setenv "PATH" (shell-command-to-string "source ~/.bashrc; echo -n $PATH"))

more detail, see: How to make emacs to run my ~/.bash_profile

Upvotes: 5

polyglot
polyglot

Reputation: 10155

Digging through the elisp source; seems like the shell-command in emacs just executes bash -c "command" in my case. Solution is to start emacs (emacs &) in a shell where my .profile is already executed (instead of starting emacs from the GUI menus of my windows manager).

Upvotes: 2

Related Questions