How to make Emacs use my .bashrc file?

A more general solution (to set all variables and aliases, not just PATH) is given in https://stackoverflow.com/a/12229404/1190077 -- the key is to set:

(setq shell-command-switch "-ic")

which overrides the default "-c" (execute the following command). The addition of "-i" forces the bash shell into interactive mode, which leads to the sourcing of ~/.bashrc.


Here's a trick I use to ensure my GUI Emacs always sees the same $PATH that I get inside a shell:

(defun set-exec-path-from-shell-PATH ()
  (let ((path-from-shell (replace-regexp-in-string
                          "[ \t\n]*$"
                          ""
                          (shell-command-to-string "$SHELL --login -i -c 'echo $PATH'"))))
    (setenv "PATH" path-from-shell)
    (setq eshell-path-env path-from-shell) ; for eshell users
    (setq exec-path (split-string path-from-shell path-separator))))

(when window-system (set-exec-path-from-shell-PATH))

Specifically, on OS X, a graphical Emacs will not pick up the user's shell's definition of $PATH, so this trick helps me on that platform.

Update: this code has now been published as an elisp library called exec-path-from-shell and installable packages are available in MELPA.

Tags:

Emacs

Bash

Path