6

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.

Emacs User
  • 5,553
  • 18
  • 48
foki
  • 886
  • 8
  • 22
  • 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](http://unix.stackexchange.com/questions/3052/alternative-to-bashrc/3085#3085). – 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 07: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 09: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 [apple.se] or [unix.se]. Or you're running some different Unix variant and you could ask on [unix.se] with all suitable details. Or you're running a non-Unix-like OS and you could ask on [su] 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

1 Answers1

7

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:

(exec-path-from-shell-initialize)

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.

Emacs User
  • 5,553
  • 18
  • 48