1

First of all, aw two things to make clear:

  • Yes I've seen there are already several questions with a similar sounding headline, but they are neither talking about the same situation nor do they have a satisfactory answer.
  • When I mean "bash prompt" I'm not talking about the prompt my terminal/shell gives me, but a read prompt inside an interactive script. I don't experience this issue only inside a script, not in the "normal" shell prompt (in my case: macOS Terminal Application, Bash 5.0).

That being said, I have user prompt inside my script. Let's say, it looks like the following:

read -rp "Enter your name: " name

So far, so good. As long as the user doesn't make a spelling mistake this probably works without any issues. Yet, when the user wants to correct something they better not use the arrow keys. If they do, contrary to their expectations, the curser won't move, but weird characters appear:

Enter your name: Sanantha^[[D

Is there a way to avoid these - what I've learned from the other threads - ANSI codes from appearing? The expected behavior would be to actually move the curser to the left by one character, each time the "left" key is pressed.

As this is a script to be distributed, it would be best, if this can be fixed inside the script and doesn't require messing with config files.

Kind regards!

Max
  • 11
  • 1
  • 2
    Would `read -erp "Enter your name: " name` work for you? – Freddy Feb 29 '20 at 22:03
  • Yes! That works for me. Thank you very much, Freddy! Never thought it would only be one character that's missing, but this is actually the solution. – Max Feb 29 '20 at 23:12
  • Can you tell me, what is "aw"? – ctrl-alt-delor Feb 29 '20 at 23:16
  • 2
    Nice! Then please write a short [self-answer](https://unix.stackexchange.com/help/self-answer) and earn some reps. You can accept your answer after two days. Thank you! – Freddy Feb 29 '20 at 23:20
  • 2
    Strictly speaking, this isn't _avoiding_ anything, as the control sequences are still sent and still read. What you are really asking for is how to get `read` to use the Bourne Again shell's own line editor instead of the canonical input mode of terminal I/O that is provided by the kernel. – JdeBP Mar 01 '20 at 00:57

0 Answers0