Answers:
用\ r回车
seq 1 1000000 | while read i; do echo -en "\r$i"; done
来自人的回声:
-n     do not output the trailing newline
-e     enable interpretation of backslash escapes
\r     carriage return
              回车本身仅会将光标移动到行的开头。如果每行新输出至少与上一行一样长就可以,但是如果新行较短,则前一行将不会被完全覆盖,例如:
$ echo -e "abcdefghijklmnopqrstuvwxyz\r0123456789"
0123456789klmnopqrstuvwxyz
要实际清除新文本的行,您可以在\033[K后面添加\r:
$ echo -e "abcdefghijklmnopqrstuvwxyz\r\033[K0123456789"
0123456789
              \e[K而不是\033[K。
                    \033[Gio \r,\033[K尽管显然\r要简单得多。另外,invisible-island.net / xterm / ctlseqs / ctlseqs.html比Wikipedia提供了更多详细信息,该信息来自xterm开发人员。
                    只要线的长度不超过端子的宽度,Derek Veit的答案就行得通。如果不是这种情况,下面的代码将阻止垃圾输出:
在第一次编写该行之前,请执行
tput sc
保存当前光标位置。现在,每当您要打印行时,请使用
tput rc
tput ed
echo "your stuff here"
首先返回到保存的光标位置,然后从光标到底部清除屏幕,最后写入输出。
tput sc # save cursor  echo '' > sessions.log.json while [ 1 ]; do     curl 'http://localhost/jolokia/read/*:type=Manager,*/activeSessions,maxActiveSessions' >> sessions.log.json     echo '' >> sessions.log.json     cat sessions.log.json | jq '.'     tput rc;tput el # rc = restore cursor, el = erase to end of line     sleep 1 done
                    tput ed而不是tput el。@Um的示例已使用ed(也许他在您评论后将其修复了)。
                    \ 033方法对我不起作用。\ r方法有效,但实际上并不会删除任何内容,只是将光标置于行的开头。因此,如果新字符串比旧字符串短,那么您可以在行尾看到剩余的文本。最终,tput是最好的方法。除了游标之外,它还有其他用途,而且它已预装在许多Linux和BSD发行版中,因此它应可用于大多数bash用户。
#/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
这是一个倒计时脚本:
#!/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"
              使用回车符:
echo -e "Foo\rBar" # Will print "Bar"
              
for i in {1..100000}; do echo -en "\r$i"; done避免后续呼叫:-)