I would like to build a shell command using elisp and then printing the result so it is stored in a #+RESULTS block and from there execute that result block as a shell command still inside the org-mode document.

How do I achieve that?


Maybe the header argument :wrap can help, e.g.,

#+BEGIN_SRC elisp :wrap src sh
(format "echo %s" (shell-quote-argument user-full-name))

#+BEGIN_src sh
echo Xu\ Chunyang

: Xu Chunyang
  • 1
    I often use the :wrap "src json" option for a org mode source block, when the output is JSON. Jul 23 '19 at 7:32

I give you an example org-file below. The emacs-lisp source block on top prints the shell source block inclusive the limiters #+begin_src sh and #+end_src.

The result format of the emacs-lisp source block is raw. That way the generated shell source block is directly embedded into the org file.

If you evaluate the org buffer with M-x org-babel-execute-buffer the Elisp source block and the updated shell source block are executed.

#+begin_src emacs-lisp :results raw
(princ "#+begin_src sh
echo \"Hello World.\";\n#+end_src")

#+begin_src sh
echo "Hello World.";

: Hello World.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.