Overwrite previous output in Bash instead of appending it
Basically the same as aneeshep's answer, but uses Return (\r
) rather than Backspace (\b
) because we don't know if the length will always be the same, e.g. when $sek < 10
.
Also, your first echo
should use $sek
, not hard-code 60
.
Finally, note the space after the ...
.
#!/bin/bash
sek=60
echo "$sek Seconds Wait!"
while [ $sek -ge 1 ]
do
echo -ne "One Moment please $sek ... \r"
sleep 1
sek=$[$sek-1]
done
echo
echo "ready!"
#!/bin/bash
sek=60
echo "60 Seconds Wait!"
echo -n "One Moment please "
while [ $sek -ge 1 ]
do
echo -n "$sek" #print sek
sleep 1
sek=$[$sek-1] #update sek
echo -en "\b\b\b" #'print' backtrace
done
echo
echo "ready!"
With bash you can use the special variable SECONDS
.
#BASH
SECONDS=0;
while sleep .5 && ((SECONDS <= 60)); do
printf '\r%s: %2d' "One moment please" "$((60-SECONDS))"
done
printf '\n'