according to my limited knowlege i'm not sure if i wrote the right title for this question.
anyhow i want to know how to the bash detect the pressed key and do something according to the script structure ,
until now i know the read somthing ; do_somthing_to $something
i want a script similar to the folowing :

while read a
    echo "$a pressed"

but i wanted it to give me the results at press time (it shouldn't wait me to hit enter)!

and thanks!


You can specify the number of characters to read with -n. So, in your case:

#! /bin/bash
read -n1 a
echo "$a pressed"

You might need to include -s to avoid printing the key when pressed.

  • thanks a lot , but it shows me the key doubled as "aa pressed" , does it have a solution or i should deal with it like that ? – Jonah Nov 6 '15 at 15:14
  • @younes: Have you read the last line? – choroba Nov 6 '15 at 15:14
  • oh okay :D ! thanks a lot brother :3 – Jonah Nov 6 '15 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.