If I start GUI emacs in OSX (not from the terminal) it does not know about my user environment variables.

The exec-path-from-shell package mostly solves this, but while you can explicitly initialize specific environment variables, I can't see a way to initialize all environment variables.

I use environment variables to supply secrets to my applications, so I tend to have a lot of them and would prefer not to have to add and remove them from my init.el.

  • Which shell are you using? Most shells have a command to list the set variables, e.g. in bash it is set – Andrew Swann Mar 1 '16 at 15:21
  • @AndrewSwann I'm using zsh. – Dean Mar 1 '16 at 18:49
  • set in zsh, without any argument, also just produces a list of variables – Andrew Swann Mar 9 '16 at 13:16

Here's what I could come up with:

(defun source-file-and-get-envs (filename)
  (let* ((cmd (concat ". " filename "; env"))
         (env-str (shell-command-to-string cmd))
         (env-lines (split-string env-str "\n"))
         (envs (mapcar (lambda (s) (replace-regexp-in-string "=.*$" "" s)) env-lines)))
    (delete "" envs)))

(exec-path-from-shell-copy-envs (source-file-and-get-envs "~/.profile")))

It sources the given file, then gets just the environment variable names from env.


I can't see a way to initialize all environment variables.

(let ((bim (shell-command-to-string ". ~/.bashrc; echo -n $BIM")))
  (setenv "BIM" bim))

Something like this should do the trick. You'll probably want to add a loop.

  • This looks like I'd still need to list out all of the environment variables I want. I have roughly 50 custom variables set, and want to use all of them without having to name each one. – Dean Mar 1 '16 at 0:14
  • The command env will list out all environment variables and their values. – verdammelt Mar 1 '16 at 2:12

Your Answer

