Posted By

niglesias on 05/20/11


Tagged

buffer Bash clear empty read keyboard


Versions (?)

Empty keyboard buffer


 / Published in: Bash
 

Empties the keyboard buffer. Useful to discard keyboard input during a command execution inside a bash script. Try this script without this line:

read -t 0.1 -N 255

and enter any garbage ([ENTER] and all) during these 2 seconds window...

The modifiers: -t 0.1 Tiemout read on 0.1 seconds - so read won't wait to get all 255 characters -N 255 Read 255 characters, treating any special character ([ENTER], tabs, etc.) as normal characters

  1. #!/bin/bash
  2.  
  3. echo -n "Type garbage..."
  4. sleep 2
  5. echo done.
  6.  
  7. # Empty keyboard buffer
  8. # 255 characters should suffice...?
  9. read -t 0.1 -N 255
  10.  
  11. echo -n "Enter value for a: "
  12. read a
  13. echo "You entered: $a"

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: niglesias on May 20, 2011

This method works only if the shell has a compatible read implementation... ┬┐anyone has a more generic method to empty the keyboard buffer?

You need to login to post a comment.