Emit zero-width bash prompt sequence from external binary

I figured it out. Bash special-cases \e, \[, and \] within PS1. It coverts \e to an escape byte, \[ to a 1 byte, and \] to a 2 byte. External commands must write 1 and 2 bytes to stdout.

According to ASCII, these encode "start of heading" and "start of text."

http://www.columbia.edu/kermit/ascii.html

Here's a working example, which relies on printf converting \ escapes within the first positional parameter into the correct bytes:

PS1='$( render-prompt )'
function render-prompt {
  printf '\1\033[0;35m\2$ \1\033[00m\2'
}
render-prompt | hexdump -C
00000000  01 1b 5b 30 3b 33 35 6d  02 24 20 01 1b 5b 30 30  |..[0;35m.$ ..[00|
00000010  6d 02                                             |m.|
00000012

Tags:

Bash

Prompt