Reputation: 245
The script:
#!/bin/bash
var=$1;
while [[ "$var" -ge "0" ]];
do
echo -ne "$var"\\r;
var=$((var-1));
sleep 1;
done
It works fine unless I pass the argument 10
(that is var=10), in that case the countdown shows: 10, 90, 89, 88,...
I also tried var=$[var-1];
and even tried to store the variable in a temporary file and and reading it from there while updating it in the loop, same strange behavior! Why is it not working?
Linux version: Debian Wheezy
Upvotes: 0
Views: 62
Reputation: 826
It's not bash freaking out. It's the way TTY prints your values. You are sending \r
which is carret return
code. This way you print the number and the carret is moved back to the beginning of the line. So indeed the echo
prints 10
, 9
, 8
, .. but it does not erase the second char. Try patching your code like this:
echo -ne "$var "\\r;
and look at the output. You could also change \r
to \n
and see what happens then.
Upvotes: 1