How to delete and replace last line in the terminal using bash?
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
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