my question is very similar to this one.

I use my .profile file to set environment variables, such as EDITOR. However, when I run ansi-term and type printenv I can see that that variable is not defined.

I am already using exec-path-from-shell but it looks like certain environment variables are not set.

Apparently the .profile file is only supposed to be read on login, but this means that environment variables that I do need to be set are not set for ansi-term which is my preferred way to use a shell in emacs.

Simply put I am looking for a way to make the environment in ansi-term identical to the a login shell. Is there a way to do this?

  • Normally .profile is read on login, and so your environment variables would be set in Emacs (and therefore in all Emacs subprocesses including the ones started from ansi-term). If your environment variables aren't set in Emacs then there's something wrong with your system configuration, it isn't Emacs fault. Are you running OSX? It manages user sessions differently; if you're running OSX you should look at OSX documentation/tutorials to see how to set environment variables. Commented Apr 14, 2017 at 7:24

1 Answer 1


Apparently the .profile file is only supposed to be read on login

Correct. Invariably there are other shell resource files which are read on non-login shell invocations, however.

If your shell is bash then you can use ~/.bashrc for this, and including something like the following in your ~/.profile is then fairly common if you want your non-login settings to be used in your login shells as well:

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"

You should read the INVOCATION section of the bash man page for full details. In Emacs you can use g INVOCATION in the man page buffer, after M-x man RET bash RET

Of course if your shell is something different, you should check its man page for the relevant details.

  • So, to put it simply I am looking for a way to make ansi-term behave just like a login shell, is that possible?
    – Startec
    Commented Apr 14, 2017 at 6:05
  • 1
    Well firstly ansi-term is a terminal emulator, not a shell. You can run a program inside it (e.g. bash), but by default you cannot pass command-line arguments to that program (e.g. bash -l would be useful, as that tells bash to act like a login shell), so you would need to write a wrapper command to do this (either an elisp command, or a shell script which invoked your shell with the desired arguments).
    – phils
    Commented Apr 14, 2017 at 8:24
  • 1
    Refer to emacs.stackexchange.com/q/18672 regarding writing elisp wrapper commands for running alternative programs (possibly with command line options) in a term buffer.
    – phils
    Commented Apr 14, 2017 at 8:30
  • 1
    If you wish to instead provide a shell script which invokes bash -l or similar, then you can manipulate the default program suggested by ansi-term by setting any of the following: explicit-shell-file-name variable; ESHELL environment variable; SHELL environment variable.
    – phils
    Commented Apr 14, 2017 at 8:34
  • setting explicit-shell-file-name seems to only work when I call shell not ansi-term. Additionally it doesn't seem to accept the -l option.
    – Startec
    Commented Apr 14, 2017 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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