13

I wrote an alias for a huge command and stored it in .bash_profile and to my surprise, emacs didn't pick up the alias that I wrote in .bash_profile. After some searching in the internet, I created a .bashrc file in my $HOME with the alias command and only after that emacs picked up the alias. I'm confused because terminal.app takes alias from .bash_profile but emacs takes alias only in .bashrc.

I was running M-x shell and I'm on macOS. Can someone explain me what's the relationship with emacs and .bashrc & .bash_profile.

More to the point, how can I get Emacs to read my .bash_profile in addition to .bashrc?

Dan
  • 32,980
  • 7
  • 102
  • 169
Chakravarthy Raghunandan
  • 3,192
  • 2
  • 19
  • 43

2 Answers2

17

This is the correct behaviour. .bash_profile is for so-called login shells. Like when you log in to your computer in text mode, or in a terminal emulator to a different computer via ssh or telnet or ...

.bashrc is meant for non-login shells, like when you are already logged in and start a new xterm, or in this case emacs' shell mode.

Usually the .bash_profile contains commands to read in the .bashrc, too:

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

so the .bashrc is read at every startup.


So, aliases belong into .bashrc; it is also customary to create a separate .bash_aliases and include it in .bashrc via the same construct:

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi
Vera Johanna
  • 883
  • 6
  • 9
13

In addition to the points made by @pingi, you can also use a separate configuration file that will be loaded only for the emacs shell (M-x shell):

From the manual page (emacs) Interactive Shell:

Emacs sends the new shell the contents of the file ‘~/.emacs_SHELLNAME’ as input, if it exists, where SHELLNAME is the name of the file that the shell was loaded from. For example, if you use bash, the file sent to it is ‘~/.emacs_bash’. If this file is not found, Emacs tries with ‘~/.emacs.d/init_SHELLNAME.sh’.

This is useful if you want to use a different shell prompt format within Emacs, or to define functions for passing files to emacsclient.

Tyler
  • 22,234
  • 1
  • 54
  • 94