Bash 脚本箭头键产生奇怪的字符 [英] Bash script Arrow Keys produces weird characters

查看:16
本文介绍了Bash 脚本箭头键产生奇怪的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下最小化的 Bash 脚本:

Consider the following minimised Bash Script:

echo Enter your name:
read NAME
echo $NAME

现在,如果我运行脚本并输入一个名称并想用箭头键浏览我的输入,则返回 [[D^ 个字符.

Now if I run the script and enter a name and want to navigate through my input with my arrow keys, [[D^ characters are getting returned.

您将如何重写该脚本以适应此类行为,即让我使用按键导航而不是赢得 ASCII 竞赛?

How would you rewrite that script to cater for such behaviour, i.e. let me navigate with keys instead of winning an ASCII contest ?

推荐答案

这些字符序列是终端传达左光标"已被按下的方式.如果接收它的程序没有这样解释它,而只是显示它们(在过滤转义字符后),那就是你得到的.

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.

幸运的是,bash 的 read 命令具有选项 -e 以启用使用 Readline 进行行内读取.Readline 执行所有这些处理(就像它在正常的 bash 命令输入中所做的那样).

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).

这篇关于Bash 脚本箭头键产生奇怪的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆