如何在elisp中捕获shell命令的标准输出?

时间:2011-02-16 08:42:09

标签: emacs elisp stdout capture

我想在Emacs中运行shell命令并将完整输出捕获到变量。有没有办法做到这一点?例如,我希望能够通过以下方式将hello-string设置为"hello"

(setq hello-string (capture-stdout-of-shell-command "/bin/echo hello"))

函数capture-stdout-of-shell-command是否存在,如果存在,它的真实名称是什么?

2 个答案:

答案 0 :(得分:57)

shell-command-to-string是否符合您的目的?
例如:

(shell-command-to-string "/bin/echo hello")

希望这有帮助。

答案 1 :(得分:16)

我有一个建议,这延伸了Ise Wisteria的答案。尝试使用这样的东西:

(setq my_shell_output
  (substring 
    (shell-command-to-string "/bin/echo hello") 
   0 -1))

这应该将字符串"hello"设置为my_shell_output的值,但要干净利落。使用(substring)消除了当emacs调用shell命令时往往会发生的尾随\n。它在Windows上运行的emacs中困扰我,也可能在其他平台上发生。