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
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.