Why do (shell-command-to-string "echo $PATH") and echo $PATH in eshell differ from echo $PATH from ansi-term and gnome terminal in Ubuntu?

Some paths are missing from (shell-command-to-string "echo $PATH") and echo $PATH in eshell.

Emacs apparently does not read .bashrc nor .bash_profile nor .profile. Thus, paths added to $PATH using export PATH="<some_path>:$PATH" in any of above files are not visible to Emacs.

This behavior is same when I connect to emacsclient from terminal. I'm using emacs --deamon on startup.

  • 2
    Emacs doesn't read any of the files you mention, that's not its job. But PATH elements added in .profile are visible to Emacs, because .profile is loaded when your session starts. Where did you add the “missing” elements? If it's .bashrc, just don't do that. – Gilles 'SO- stop being evil' Jul 22 '15 at 21:04
  • @Gilles This is strange. I have all of my export PATH commands listed in ~/.profile However, when I start ansi-term mode, only my .bashrc file is read (so nothing is added to my PATHs). This is different than when I have a shell outside of emacs. How can I force ansi-term to read .profile or should I use exec-path-from-shell? – Startec Jan 30 '17 at 7:59
  • @Startec The behavior you describe in Emacs is normal. .profile is supposed to be read only when you log in, not whenever you start a new shell. There must be a misconfiguration in the way you start a shell outside Emacs, that forces the shell to be a login shell. – Gilles 'SO- stop being evil' Jan 30 '17 at 9:33
  • @Gilles but you said earlier that PATH elements added in .profile are visible to Emacs, because .profile is loaded when your session starts but it looks like they are not in my case (and in the OP case). Do I really have to use exec-path-from-shell just to make the $PATH available to emacs ans ansi-term exactly the same as the one to available to bash outside of emacs? – Startec Jan 30 '17 at 19:20
  • 1
    @Startec That's a problem with your session startup then. Either you're running OSX, which has the messed-up behavior you describe by default, and I don't know how to make it right, but you could ask on Ask Different or Unix & Linux. Or you're running some different Unix variant and you could ask on Unix & Linux with all suitable details. Or you're running a non-Unix-like OS and you could ask on Super User with all suitable details. But please, let's stop hijacking foki's question for this. – Gilles 'SO- stop being evil' Jan 30 '17 at 20:26

As Gilles mentioned in the comment above, emacs does not read the local env settings unless specifically asked to. The easiest way to keep variables in shell terminals inside and outside Emacs is to use a package, such as the Steve Purcell's package exec-path-from-shell.

Once installed, this package ensures each time you start a shell in emacs, it reads the necessary local settings with this command in init.el:


I usually put this before invoking package repository commands in the init.el file so external dependencies, if any, are made available without causing missing command errors.

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.