When I run a script like read -n 1 -p 'Enter first variable: ' var1if echo ${var1} | grep -iq ^ythen    echo okayelse    echo not okayfisleep 2read -n 1 -p 'Enter second variable: ' var2if echo ${var2} | grep -iq ^ythen    echo okayelse    echo not okayfiIf I run this code, hit y and then hit n in span of 2 seconds, the n is saved in buffer and given to the next read which is why it doesn't wait and ask for user input for Enter second variable:, it simply gives the key in buffer i.e n here.For making myself clear, I am paraphrasing my question in different words in the following questions but the question is just one.How do I stop keys being buffered to the memory ? ORHow do I ignore the user input in the buffer for second variable var2 ? ORHow do I disable/ignore keyboard input in between my first and second read so that whatever I type in the span of 2 seconds after the first read, it does not go as an input to the second read
How to ignore keyboard input buffer in between two `read`
shell script;scripting;keyboard
You'd want to call the tcflush() POSIX function, to flush the pending input.For instance with:perl -MPOSIX -e 'tcflush 0,0'