In addition to \r
or \b
approaches, it's possible to use the \033[2K
control character, which tells the terminal to clear the whole line. The advantage of this compared to \b
is that you don't have to match number of \b
with the amount of characters you want to delete, and compared to \r
there won't be characters sticking out on the screen if the new line is shorter than the old one.
Below is the example of how it can be applied to this question, and here is an example of the related application to create output similar to boot messages. In this particular example, the timer will be gone once 0th second is reached and the timer line will be replaced with "Ready!" phrase.
#!/bin/bash
sek=60
echo "60 Seconds"
while ((sek--)); do
printf "One moment please: %d" "$sek"
sleep 1
printf "\r%b" "\033[2K"
done
echo "Ready!"
Another alternative would be to employ dialog
command for creating simple dialogs in command-line. The dialog will remain on screen for the duration of the timer and update with the loop, and by the time it's done - the timer will be replaced with "Ready! Press to exit" message in a seamless manner:
#!/bin/bash
sek=60
echo "60 Seconds"
while ((sek--)); do
echo "$sek" | dialog --progressbox "Please wait" 10 25
sleep 1
done
dialog --msgbox "Ready! Press <OK> to finish" 10 25
watch
command, although I'm not sure exactly how to do it.