I'm trying to compile u-boot via Emacs' compilation mode, and it looks like Emacs doesn't know how to find bash environment variables. Even though I set them, and can compile via Emacs shell emulation, compilation mode still tries to compile as if they aren't there.
What do I need to do to make it more environment conscious?
The easiest way to set environment variables in Bash is to use the “export” keyword followed by the variable name, an equal sign and the value to be assigned to the environment variable.
Under bash shell: To list all the environment variables, use the command " env " (or " printenv "). You could also use " set " to list all the variables, including all local variables.
You can set your own persistent environment variables in your shell configuration file, the most common of which is ~/. bashrc. If you're a system administrator managing several users, you can also set environment variables in a script placed in the /etc/profile.
You can try adding something like to your .emacs
:
(let ((path (shell-command-to-string ". ~/.bashrc; echo -n $PATH")))
(setenv "PATH" path)
(setq exec-path
(append
(split-string-and-unquote path ":")
exec-path)))
Depending on whether you've set the env variables in .bash_profile
or .bashrc
you might need to slightly adjust this snippet. The example is for the PATH
variable, which is a bit more special (since you have to set exec-path
in Emacs as well), but can be extended to work for arbitrary variables - you could have a list of variables that have to be read from .bashrc
and set into Emacs.
I'm not sure whether you're using OS X or GNU/Linux. Starting Emacs from the GUI's menu-bar in Linux will typically result in an Emacs that does not have the same PATH
as one launched from the command line. This problem dates back to the first xdm
Xsession scripts, and while they are fairly easy to fix (basically use an Xsessionwrapper
script that does exec $SHELL -c Xsession
so the shell gets run before running the user's Xsession), nobody has bother to do so in a very long time (and I doubt that anyone will). As far as I know the problem is present even into moder xdm descendants such as kdm
and gdm
.
On OS X the handling of the env variables is another problem entirely and to get your ENV variables you typically have to run Emacs from the command line like this /Applications/Emacs.app/Contents/MacOS/Emacs
or play with ~/.MacOSX/environment.plist
. The code snippet I've provided should cover you in both cases though.
Update
Recently this process was made easier by the exec-path-from-shell extension. It sets the emacs $PATH
in more or less the same manner, but using an extension is generally preferable to hacking the solution yourself.
This is where the environment variables of the process that started emacs
are:
— Command: getenv var
This function returns the value of the environment variable var, as a string. var should be a string. If var is undefined in the environment, getenv returns nil. It returns ‘""’ if var is set but null. Within Emacs, a list of environment variables and their values is kept in the variable process-environment.
(getenv "USER") ⇒ "lewis"
— Variable: process-environment
This variable is a list of strings, each describing one environment variable. The functions getenv and setenv work by means of this variable.
process-environment ⇒ ("PATH=/usr/local/bin:/usr/bin:/bin" "USER=lewis" "TERM=xterm" "SHELL=/bin/bash" "HOME=/home/lewis" ...)
You seem to be assuming that emacs was started from a bash session. However, often processes under X are started from an sh session, which would not read the environment variables you had set in your ~/.bashrc
script. One simple way to circumvent this is to change your ~/.xinitrc
file to use bash instead of sh (it could be as simple as adding #!/bin/bash
at the top of the file).
Source: gnu.org
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With