Konubinix' opinionated web of thoughts

Make Org-Babel-Sh-Evaluate Handle No Newline Input Lines Correctly


For the analysis, see how org babel processes the output in shell’s sessions.

Simply put, the problem is that:

  • echo -n a
  • echo -n b


  • a$b$org-babel-sh-eoe\n$

Or, once concatenated


In fact, the code as no way of knowing that the prompt is $ and not a$b$.

In that case, we could try making the comint-prompt-regexp value fit exactly the PS1 value, instead of using a lax regexp.

By setting the value of the prompt in the session buffer

(with-current-buffer "c66d4791-003d-42af-a4e0-366f5542c604"
  (setq comint-prompt-regexp ":theprompt:")

And using this prompt in the session

export PS1=":theprompt:"

Then the above snippet should work better

echo -n a
echo -n b

Great, now the output is shown almost correctly. There is an extra newline though, we would prefer the output ab. Unfortunately, because org-babel-sh-evaluate concatenates with \n the results of the string that was split with the prompt, we lost the information whether the output had a newline or not. Fixing this would require to rethink the interface between org-babel-sh-evaluate and org-babel-comint-with-output. This would need to rethink the interface with everything using org-babel-comint-with-output and don’t have time for this, I barely use output not ending with newline. So I won’t try to fix this today.

I don’t know of an easy way to customize PS1 or the comint-prompt-regexp, so I suggest using the following snippets on top of the notes using multi line bash commands.

Evaluate those in order.

(with-current-buffer session    (setq comint-prompt-regexp ":theprompt:")    )

export PS1=":theprompt:"

Notes linking here