Palace Chan
Palace Chan

Reputation: 9183

How to actually change/set environment variables from within emacs

In my shell I did: export BLA=foo and then I do echo $BLA and I see foo as expected. I then load up emacs and do M-! for a shell command and do echo $BLA and it's set to something else, bar. So then I run M-x setenv and give it BLA and foo at the prompts but when i do the echo i still see bar. Why would that be and how can I change it? I am trying to do this for some environment variables under which I want to run M-x compile

Upvotes: 8

Views: 9336

Answers (2)

Alfe
Alfe

Reputation: 59426

Your shell in which you've started the Emacs hands over a copy of its environment to its child process (the Emacs), that's how the value is transmitted from the shell to the Emacs. Any change the Emacs then performs with its inherited environment will only affect the Emacs process's environment. There is no way the Emacs's environment can influence the environment of the shell.

If you need to hand information back to the shell, you have to use different techniques like temp files, named pipes, sockets, …

If you just want to check the environment of the Emacs itself, use M-x getenv to look at variables, or use M-! echo $BLA. If this also is showing sth else, then you probably have a special BLA which is automatically set to sth after each command, or which isn't writable at all like RANDOM or similar.

Upvotes: 4

juanleon
juanleon

Reputation: 9380

setenv will modify the environment for the processes emacs launch after you set the value. Running child processes will not be affected.

Thus, doing a (setenv "FOO" "bar") and then M-x shell (provides you have not a running shell yet) will produce a shell with environment variable "FOO" set to "bar".

Upvotes: 15

Related Questions