Sometimes in Bash, you want to display some text in the console and sometimes you may also want to have the ability to overwrite the line that you have just displayed with a new string.

For example, a bash script could display a text “Working..” on the console and then, instead of display “Finished!” to the next line, you want the line that first display “Working…” be cleared and replace with the “Finished!” string.

Here is a little piece of code that could do this:

#!/bin/bash
tput sc
echo -n “Working…”
for i in `seq 1 5000`;
do
echo $i > \dev\null
done
tput el1
tput rc
echo “Finished!”

Update There's actually a simpler way:

for i in `seq 1 5000`; do
    echo $i > /dev/null               # do some processing
    echo -n -e "working $i\x0d"
done
On 20 August 2010

Comments