How to capture standard output of a shell command in elisp?

Ryan C. Thompson picture Ryan C. Thompson · Feb 16, 2011 · Viewed 11.5k times · Source

I want to run a shell command within Emacs and capture the full output to a variable. Is there a way to do this? For example, I would like to be able to set hello-string to "hello" in the following manner:

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

Does the function capture-stdout-of-shell-command exist, and if so what is its real name?

Answer

Ise Wisteria picture Ise Wisteria · Feb 16, 2011

Does shell-command-to-string meet your purpose?
For example:

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

Hope this helps.