Bash Read Escaped Characters and Normal Characters

主宰稳场 提交于 2020-01-25 00:09:06

问题


I want to read in bash when any key is pressed, be it arrow key, number, letter, or punctuation, without pressing enter.

This is the closest that I have come up with, besides that when an escaped key is pressed, it spills over into the next input. Also, escaped keys will read but not echo.

#!/bin/bash

read -r -n 1 -d $'\n' -p 'input> ' line
echo -en "\n"
echo "$line"

回答1:


A bit hacky/dirty way, but should work for user interactive shells...

read -n1 -s -p 'input> ' line; read -t 0.1 -n2 -s line2
line="$line$line2"

Now, it's up to you to convert <ESC>[A to string <UP> or not.

NOTE: It would most likely fail, if the stdin is redirected (say, from pipe/file...)



来源:https://stackoverflow.com/questions/15712430/bash-read-escaped-characters-and-normal-characters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!