1

I am trying to make a timer in Bash which countdowns 20 minutes and then starts a 10 minute break immediately after the previous. Then the loop continues and returns again to 20 minutes.

#!/bin/bash
MIN=20
BREAK=10
while [[ 0 -ne $MIN ]]; do
    echo "$MIN"
    sleep 1
    MIN=$[$MIN-1]
done
WHILE [[ 0 -ne $BREAK ]]; do
    echo "$BREAK"
    sleep 1
    BREAK=$[$BREAK-1]
done

I made it with seconds here. So 20 seconds pass by and then the 10 seconds start immediately. But when the 10 seconds expire the script stops. Is there a way to start again with 20 minutes?

Thank you!

2

As you know, to repeat a block of code you use a while do ... done loop. So to start all over again you just need to wrap the entire segment of code in another while... loop.

This time the condition can be just the command true. So you'd get this:

while true
do
    # Put the code to be repeated forever in here
    :
done
| improve this answer | |
  • I was close to solving it but I was missing something. Thank you! – Dankata Jul 20 '17 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.