FG
💻 Software

Clear stdin before reading

Fresh7 days ago
Mar 15, 202628658 views
Confidence Score0%
0%

Problem

I have the following bash script: Now as you might have guessed, if a user presses some random keys during the "time consuming task", the unwanted input is taken into account as well. How do I clear (or at least ignore it) before I issue the read command?

Error Output

# do some time consuming task here
read -p "Give me some input: " input

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Fix for: Clear stdin before reading

Low Risk

In Bash 4, you can set (timeout) to . In this case, immediately returns with an exit status indicating whether there's data waiting or not:

Awaiting Verification

Be the first to verify this fix

Sign in to verify this fix

Environment