0

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?

Startec
  • 1,354
  • 1
  • 13
  • 30
  • 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. – Gilles 'SO- stop being evil' Apr 14 '17 at 07:24

1 Answers1

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"
    fi
fi

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.

phils
  • 48,657
  • 3
  • 76
  • 115
  • 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 Apr 14 '17 at 06: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 Apr 14 '17 at 08:24
  • 1
    Refer to https://emacs.stackexchange.com/q/18672 regarding writing elisp wrapper commands for running alternative programs (possibly with command line options) in a term buffer. – phils Apr 14 '17 at 08: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 Apr 14 '17 at 08: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 Apr 14 '17 at 09:48
  • Maybe your version of Emacs doesn't look for that variable then. It works for 24.4+ at least. And yes, as I mentioned before, you *cannot* specify command line options in the `program` argument to `ansi-term`. – phils Apr 14 '17 at 12:05