i want an if statement to only run if the condition in the statement is true for at least 3 consecutive seconds, how do i achieve it in linux bash shell script? i haven't done it before, and couldn't find anything specific by googling and would greatly appreciate any help! i don't mean like a loop that executes the condition x amount of times, but a function that checks of the condition is true for at least x consecutive seconds/minutes before it runs a block of code.

thanks in advance

  • Has any of the answers solved your problem? Please accept it (✓) or clarify your needs using editing or comments. You can also upvote (▲) useful answers, even if you have not the earned privilege yet (your votes won’t be counted until you get the privilege though).
    – Melebius
    Feb 19, 2020 at 7:15

2 Answers 2


This is not possible, you have to stick with the loop, or at least test, sleep 3, test again.


Computers (digital ones) work with the discrete time, they don’t understand continuous time. In the discrete time, a condition “remains” true if it’s true in every sample. So you have to run a loop and count successful samples in it.

If you want to make the loop depending on the clock time rather than the repeat count, store the start time and test whether the expected time has elapsed in the loop condition.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .