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,584
  • 6
  • 98
  • 168
Chakravarthy Raghunandan
  • 3,132
  • 2
  • 18
  • 42
  • 1
    How did you get Emacs to pick up your `.bashrc` aliases? I'm asking this because my Emacs (and [apparently](https://stackoverflow.com/questions/163591/bash-autocompletion-in-emacs-shell-mode) others') doesn't do it by default. – Arch Stanton May 08 '18 at 22:20
  • @ArchStanton see @Vera Johanna answer to this question. That is the same method I used. Though now I'm using `zsh` instead of bash :) – Chakravarthy Raghunandan May 09 '18 at 06:26

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
  • 873
  • 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
  • 21,719
  • 1
  • 52
  • 92