Emacs Lisp spawning subprocesses with custom environmental variables
Basically I want to spawn a process changing its working directory. My idea was to spawn a process and set the PWD 开发者_运维问答enviroment variable.
There's a way to obtain something like that?
(virtually I would like to change any of the environment variables for flexibility)
Just bind the default-directory
variable before spawning your process. Example:
(let ((default-directory "/tmp/"))
(call-process "/bin/bash" nil "*scratch*" nil "-c" "echo working dir is $PWD"))
I ran this from my *scratch*
buffer and the string working dir is /tmp
was added to the buffer.
Do you mean like this?
This shows the default environment variables:
(shell-command "env")
and this shows how you could change them on a per-process basis:
(shell-command "PWD=/tmp env")
$PWD
is an environment variable that reflects the current working directory, not one that controls it.
According to the Processes section of the Elisp manual, the default directory for a spawned process is determined by the value of default-directory. The environment of the spawned process is the same as that of the Emacs session, which you can modify with setenv. You can also specify environment variables which are used solely for spawned processes via the process-environment variable.
For what it's worth, to get python subprocesses to behave the way that I wanted (apparently completely thinking that they were in the specified directory) I had to do:
(let* ((default-directory (find-correct-dir))
(process-environment (cons (format "PWD=%s" default-directory)
process-environment)))
(do-the-subprocess-thing))
Or, if I wanted to throw away the rest of the environment:
(let* ((default-directory (find-correct-dir))
(process-environment (list (format "PWD=%s" default-directory))))
(do-the-subprocess-thing))
That is, I needed to set both default-directory
and process-environment
, and to the same value.
精彩评论