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

查看:65
本文介绍了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天全站免登陆