1

To prevent the prompt from warping around right-to-left text, I want to insert a zero width LRM into the bash prompt (U200e). Since this is a zero width character, my instinct was to wrap it with \[<200e>\]. Turns out, that actually causes bash to line wrap in the wrong place. When I don't wrap it with anything, then it seems to work fine.

Which I don't understand. Even if bash auto-detects this character is a zero width, wrapping a \[...\] around it shouldn't have hurt anything.

EDITED: So the question is: when is \[...\] escaping actually needed, and how can it possibly be harmful if the chars it escapes are actually zero width.

Shachar Shemesh
  • 245
  • 3
  • 13

1 Answers1

-1

See the post Escaping zero length characters in bash. and specifically the answer by Michael Allen which I reproduce below:

the \[ sequences are used by some utilities, including PS1, to represent the \001 and \002 control codes:

\[ => \x01 or \001

\] => \x02 or \002

printf and echo don't do this translation from \[ to \001.

So the solution was to do the conversion myself. Instead of wrapping zero length characters in \[:

echo "\[\033[1;30m\]foo\[\033[0m\]"

which would output \[foo\]

I instead output the actual control code:

echo "\x01\033[1;30m\x02foo\x01\033[0m\x02"

which outputs foo on both PS1 printf and echo.

For a concrete example see this commit on git-radar.

harrymc
  • 498,455