How to delete and replace last line in the terminal using bash?

I want to implement a progress bar showing elapsed seconds in bash. For this, I need to erase the last line shown on the screen (command "clear" erases all the screen, but I need to erase only the line of the progress bar and replace it with the new information).

Final result should look like:

$ Elapsed time 5 seconds

Then after 10 seconds i want to replace this sentence (in the same position in the screen) by:

$ Elapsed time 15 seconds

Solution 1:

The carriage return by itself only moves the cursor to the beginning of the line. That's OK if each new line of output is at least as long as the previous one, but if the new line is shorter, the previous line will not be completely overwritten, e.g.:

$ echo -e "abcdefghijklmnopqrstuvwxyz\r0123456789"
0123456789klmnopqrstuvwxyz

To actually clear the line for the new text, you can add \033[K after the \r:

$ echo -e "abcdefghijklmnopqrstuvwxyz\r\033[K0123456789"
0123456789

http://en.wikipedia.org/wiki/ANSI_escape_code

Solution 2:

echo a carriage return with \r

seq 1 1000000 | while read i; do echo -en "\r$i"; done

from man echo:

-n     do not output the trailing newline
-e     enable interpretation of backslash escapes

\r     carriage return

Solution 3:

Derek Veit's answer works well as long as the line length never exceeds the terminal width. If this is not the case, the following code will prevent junk output:

before the line is written for the first time, do

tput sc

which saves the current cursor position. Now whenever you want to print your line, use

tput rc
tput ed
echo "your stuff here"

to first return to the saved cursor position, then clear the screen from cursor to bottom, and finally write the output.

Solution 4:

The \033 method didn't work for me. The \r method works but it doesn't actually erase anything, just puts the cursor at the beginning of the line. So if the new string is shorter than the old one you can see the leftover text at the end of the line. In the end tput was the best way to go. It has other uses besides the cursor stuff plus it comes pre-installed in many Linux & BSD distros so it should be available for most bash users.

#/bin/bash
tput sc # save cursor
printf "Something that I made up for this string"
sleep 1
tput rc;tput el # rc = restore cursor, el = erase to end of line
printf "Another message for testing"
sleep 1
tput rc;tput el
printf "Yet another one"
sleep 1
tput rc;tput el

Here's a little countdown script to play with:

#!/bin/bash
timeout () {
    tput sc
    time=$1; while [ $time -ge 0 ]; do
        tput rc; tput el
        printf "$2" $time
        ((time--))
        sleep 1
    done
    tput rc; tput ed;
}

timeout 10 "Self-destructing in %s"

Solution 5:

In case the progress output is multi line, or the script would have already printed the new line character, you can jump lines up with something like:

printf "\033[5A"

which will make the cursor to jump 5 lines up. Then you can overwrite whatever you need.

If that wouldn't work you could try printf "\e[5A" or echo -e "\033[5A", which should have the same effect.

Basically, with escape sequences you can control almost everything in the screen.