How to invoke env from eLisp and escape the path to the target program properly?

£可爱£侵袭症+ 提交于 2020-01-06 08:37:30

问题


What I'm trying to do is as follows:

(let ((pj-path (concat src haxe-project-generator)))
        (when (file-exists-p pj-path)
          (shell-command
           (concat "env " (haxe-build-env-vars <more arguments>)
                   (format "'%s'" pj-path)))))

I.e. I want to call a program pj-path in environment populated with some variables.

In the line (format "'%s'" pj-path) I tried single and double quotes - but env doesn't do path expansion and if it is quoted, it will treat it as a string, not finding the file. However, if it is not quoted, then it will interpret the part of the string until the first white space as a path to program.

If I escape spaces (as in preceding them with backwards slash), it seems to work, but, surely, white spaces aren't the only character that would confuse the shell executing the command.

So, what is the proper way to escape the name of the executable which should be passed to env in eLisp?


回答1:


Try shell-quote-argument.

(shell-command (concat "ls " 
                       (shell-quote-argument "/path with spaces/(and parens)/")))


来源:https://stackoverflow.com/questions/11189439/how-to-invoke-env-from-elisp-and-escape-the-path-to-the-target-program-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!