在脚本中显示时修改字符串

时间:2016-12-04 13:49:12

标签: bash

我试图弄清楚如何编写一个bash脚本,在大约0:55执行类似这样的操作,其中字符不断变化 - / \ | https://www.youtube.com/watch?v=ZwK2NReSRFU

我怎么做这样的事情,但在做第二次之前删除了第一个echo命令?

echo "hello"
sleep 1
echo "Hello"
sleep 1
echo "hEllo"
slep 1
echo "heLlo"

等等

1 个答案:

答案 0 :(得分:1)

您需要存储和恢复光标位置并清除当前行。这是使用ANSI terminal escape sequences完成的:

echo -en "\033[s" # Save cursor position
while true ; do 
    echo -n "hello"
    sleep 1
    echo -en "\033[1K" # Clear current line
    echo -en "\033[u" # Restore cursor position
    echo -n "Hello"
    sleep 1
    echo -en "\033[1K"
    echo -en "\033[u"
    echo -n "hEllo"
    sleep 1
    echo -en "\033[1K"
    echo -en "\033[u"
    echo -n "heLlo"
    sleep 1
    echo -en "\033[1K"
    echo -en "\033[u"
    echo -n "helLo"
    sleep 1
    echo -en "\033[1K"
    echo -en "\033[u"
    echo -n "hellO"
    sleep 1
    echo -en "\033[1K"
    echo -en "\033[u"
done