0

I have an Org mode file with the following source block in it:

#+begin_src sh :exports both
echo "first line"
echo "second line"
#+end_src

#+RESULTS:
| first  | line |
| second | line |

But when I export it as HTML, I get the following output:

enter image description here

I would like to export it so that the output is interleaved with the execution of each command:

$ echo "first line"
first line
$ echo "second line"
second line

How can I do that?

5
  • One way is to have multiple source blocks, one for each command. – NickD Nov 26 '20 at 14:32
  • @NickD Yeah, that would be one way. I was hoping that there would be some tweaks to the source block which would allow interleaved output. Something like: :exports both-interleaved :-) But I guess it's not present. – Sibi Nov 26 '20 at 14:48
  • There is nothing like that: Org babel evaluates the source block by storing all the commands in a temp file, runs the file using the appropriate language interpreter, collects the output, does minor munging to it and writes it out as the results block. What you are asking for is fundamentally incompatible with the babel model. – NickD Nov 26 '20 at 14:55
  • @NickD Thanks! Can you add that as an answer ? I could delete the question, but I feel others who are beginning to explore Org babel (like me!) might get the same question. – Sibi Nov 26 '20 at 15:00
  • As you wish :-) – NickD Nov 26 '20 at 15:11
2

There is nothing like that: Org babel evaluates the source block by storing all the commands in a temp file, runs the file using the appropriate language interpreter, collects the output, does minor munging to it and writes it out as the results block. What you are asking for (a header argument that modifies this work flow) is fundamentally incompatible with the basic babel model.

EDIT: I should have added a couple of possible ways to get what you want even if a bit inconveniently:

  • one way is to have separate code blocks for each command as mentioned in the comments:
#+begin_src sh :exports both
    echo "first line"
#+end_src

#+begin_src sh :exports both
    echo "second line"
#+end_src
  • a second way is to forego the export of the code and just export results, but make the code block output the commands as well as the output of those commands:
#+begin_src shell :results output :exports results :wrap example
  cmd='echo "first line"'
  echo "\$ $cmd"
  eval $cmd
  cmd='echo "second line"'
  echo "\$ $cmd"
  eval $cmd
#+end_src
  • If you are using bash, you can use arrays to make the second method much more palatable:
#+begin_src shell :results output :exports results :wrap example

declare -a cmds=('echo "first line"'
                 'echo "second line"')

for cmd in "${cmds[@]}" ;do
    echo "\$ $cmd"
    eval $cmd
done
#+end_src

All you need to do is add more commands to the cmds array: the rest of the code does not need to be touched.

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.