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

shell, unix

Solution

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 character
echo $char         # print the character (or you can do something else with it)

Problem

I use the below code to assign a given by the user character to variable G: ``` read G ``` However in order to move forward with executing of my script I need to press enter button. Is it possible to set the read command somehow that it show process forward to the next line immediately after it receive the one letter from stdin?

Original source