printf "input: "; read -e. I type something then I press
Backspace. When reaching the last character, this deletes the
input: part together with it.
Backspace doesn't misbehave if I hadn't typed anything before or if I used simple
read (no Readline).
read -e calls the readline library. Which gives access to several editing tools that a plain
read does not have. However, it assumes an "empty line".
A workaround to this problem is to give something (like an space) to avoid the "empty line" assumption:
printf 'input:'; read -e -p ' '
But since that is using the
-p option already, it might be simpler to write:
read -e -p 'input: '
bash is a builtin command.
read -e uses the
bash full-screen editing via the
readline library (i.e. it understands how to go back to the previous line, move the cursor left and right through the inputted text, etc.). It may use cursor addressing codes to optimize the output to the screen.
However it expects that the cursor is at the beginning of the line when it begins, and sometimes it's faster to clear the whole line when removing the input than to remove the individual characters. This is what is messing up your prompt, which moves the cursor to the right without bash knowing about it.
The same thing can happen when using terminal escapes in your prompt (e.g. to set a colour, or to set the terminal title). For this purpose
bash has special escapes to indicate which bytes don't move the cursor.
You should use
read -p 'input: ' -e, the
-p option for prompting is specially made for this.