Bash script Arrow Keys produces weird characters

后端 未结 2 1971
抹茶落季
抹茶落季 2021-01-03 05:00

Consider the following minimised Bash Script:

echo Enter your name:
read NAME
echo $NAME

Now if I run the script and enter a name and want

相关标签:
2条回答
  • 2021-01-03 05:50

    These character sequences are the way that the terminal communicates that "cursor left" has been pressed. If the program receiving it does not interpret it as such and instead just displays them (after filtering the escape character), that's what you get.

    Luckily for you, the read command of bash has the option -e to enable use of Readline for reading in the line. Readline performs all that handling (as it does on the normal bash command input).

    0 讨论(0)
  • 2021-01-03 05:50

    Thanks to Andreas and some due dilligence with a search engine, I was able to rewrite my script:

    echo Enter your name:
    read -e NAME
    echo $NAME
    

    Now navigating through the input with arrow keys, works like a expected.

    Here you can learn more about the read builtin command.

    0 讨论(0)
提交回复
热议问题