Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 683
Palace Chan Avatar asked Jul 03 '13 14:07

Palace Chan


2 Answers

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

like image 83
juanleon Avatar answered Oct 22 '22 21:10

juanleon


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.

like image 37
Alfe Avatar answered Oct 22 '22 22:10

Alfe