How to set read command to execute without having to press Enter button in shell? How to set read command to execute without having to press Enter button in shell? unix unix

How to set read command to execute without having to press Enter button in shell?


If you're using Bash:

read -n1 G

Your system is configured to use dash by default and dash doesn't support this feature, so you also need to change the first line of your script to specify that you wish you use bash:

#!/bin/bash


I thought dd might be able to do this, so I tried it out and didn't get anywhere. However, Google found this for me and I adapted it here:

readOne () {    local oldstty    oldstty=$(stty -g)    stty -icanon -echo min 1 time 0    dd bs=1 count=1 2>/dev/null    stty "$oldstty"}

Define this function once at the beginning of your script, then you can use it like this:

char=$(readOne)    # get the characterecho $char         # print the character (or you can do something else with it)