purcell / exec-path-from-shell

Make Emacs use the $PATH set up by the user's shell
Other
1.41k stars 81 forks source link

Copy all environment variables #23

Closed svend closed 9 years ago

svend commented 9 years ago

It would be convenient to be able to copy all environment variables without specifying them individually. Could the list of environment variables be obtained by parsing the output of env?

purcell commented 9 years ago

I think this can't reliably be done, for the same reason that the existing code avoids calling env to print out all the environment variables: their values are allowed to contain "=", "\n" and other characters which would make the env output irregular. The existing code makes sure to print them surrounded by nulls, but there's no way to tell env to just list the names of the environment variables it knows about.

If you're aware of a way to make any POSIX-compliant shell enumerate all its exported environment variables, then it may be possible to do this.

svend commented 9 years ago

Good point. I don't know a POSIX way to do this.

GNU printenv and env support --null, but the OS X versions do not. I can get a list using gprintenv from homebrew coreutils on OS X like this:

(mapcar (lambda (s) (car (split-string s "=")))
    (split-string
     (shell-command-to-string "bash -l -i -c \"gprintenv --null\" 2>/dev/null")
     (char-to-string ?\x0)
         t))
purcell commented 9 years ago

Good to know. I really want to avoid relying on anything that won't necessarily be available on every UNIX, though. Compatibility across zsh, bash, tcsh and fish has already been surprisingly hard to achieve.

svend commented 9 years ago

Agreed. I don't have a good solution, so I'll close this.