bash:如何在同一位置回显字符串

Yve*_*ves 3 unix linux bash shell echo

我建立了我的网络服务器,我正在尝试做一个测试。所以我用 bash 脚本模拟了很多请求:

i=0
while [ $i -lt 20 ]; do
    echo ''
    echo ''
    echo ''
    echo '============== current time ==============='
    echo $i
    echo '==========================================='
    echo ''
    curl -i http://www.example.com/index?key=abceefgefwe
    i=$((i+1))
done
Run Code Online (Sandbox Code Playgroud)

这很有效,但我更喜欢echo在终端上的同一位置进行所有操作。
我读过这个:如何在同一行显示和更新回声

所以我添加-neecho但它似乎没有按预期工作。
的消息还是curl可以推开的echo

这就是我需要的:

============== current time =============== ---\
1   <------ this number keeps updating      ----> the 3 lines stay here
=========================================== ---/
Here is the messages of `curl`, which are showing as normal way
Run Code Online (Sandbox Code Playgroud)

Die*_*ano 5

还有另一种选择,在写入标准输出之前定位光标。

您可以设置xy满足您的需求。

#!/bin/bash

y=10
x=0
i=0
while [ $i -lt 20 ]; do
    tput cup $y $x
    echo ''
    echo ''
    echo ''
    echo '============== current time ==============='
    echo $i
    echo '==========================================='
    echo ''
    curl -i http://www.example.com/index?key=abceefgefwe
    i=$((i+1))
done
Run Code Online (Sandbox Code Playgroud)