Languages
[Edit]
EN

Bash - detect key pressed

9 points
Created by:
Amir-Hashempur
607

In this short article, we want to show how to handle pressed key without waiting to rerun key / enter key confirmation under Bash.

Quick solution:

Where:

  • -r disables waiting to type new line character to end read command (Return / Enter key is not required),
  • -s does not display typed characters,
  • -n 1 expects to type one character.

Alternatively you can use short version too: read -rsn1 input

 

Practical example

Edit

In this section, we want to show how to create a simple menu that doesn't require to pres Return / Enter key after the option is typed.

example menu.sh file:

Alternative titles

  1. Bash - read key without waiting to enter
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join