How to set the read command to execute without pressing the Enter key in the shell?

I use the code below to assign the given user character to the variable G:

read G

However, in order to move forward with the execution of my script, I need to press the enter button. Is there any way to set the read command so that it displays the process on the next line immediately after receiving one letter from stdin?

+3
source share
2 answers

I thought I ddcould do this, so I tried it and I won’t go anywhere. However, Google found this one 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"
}

script, :

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

Bash:

read -n1 G

, , script, , Bash:

#!/bin/bash
+5

Source: https://habr.com/ru/post/1724229/


All Articles