How to read any key in Bash?

I can get read -n 1 KEY to get most keys, with the exception of keys that are represented by multiple characters. For example, if I press the up arrow key:

 $ read -n 1; echo ^[[A $ [A 

As you can see, read accepts only Esc and [A

What I want to do in the script is:

  • Scroll through the list with the arrow keys and press Enter to do something with it.
  • For other actions, press different keys.
+3
source share
2 answers

You are better off using the dialog mentioned by jm666, but there are other ways to drop this cat.

 read -n 1 x; while read -n 1 -t .1 y; do x="$x$y"; done 

Basically, wait until you read the character, and then start consuming the input signal until 1 second has passed without input.

A warning. Fast typists can get annoyed. You may need to configure this timeout.

+8
source

Not a direct answer to your question, but a solution:

You should probably check the dialog utility to create ncurses dialogs from the shell. see http://hightek.org/dialog/

Google will generate a few examples or check out: http://unstableme.blogspot.sk/2009/12/linux-dialog-utility-short-tutorial.html

+3
source

All Articles