How to make an animated bash shell prompt for the

2020-03-25 06:10发布

问题:

I want to make an animated curser prompt in the terminal(Ubuntu 14.04),
so i make this script:

while [ : ]
do
    echo -ne '|\r'
    sleep 0.3
    echo -ne '/\r'
    sleep 0.3
    echo -ne '一\r'
    sleep 0.3
    echo -ne '\\ \r'
    sleep 0.3
    echo -ne '|\r'
    sleep 0.3
    echo -ne '$\r'
    sleep 0.3
done

But when i put it in the .bashrc ps1 ~/animated-prompt.sh & it make a text distortion and every thing i write is overridden by the \r character.

so Is there anyway to make this work ?
(IDK, like putting the script to another process thread other than the one that the terminal is working on)

回答1:

save and restore cursor position instead of \r. Move to required column location where you want animation between saving and restoring cursor position.

For cursor movement refer http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html

modified script:

s="\033[s"
u="\033[u"

# Position of column
# As per my command prompt, i want 15th column( so 14C)
pos="\033[1000D\033[14C"
while [ : ]
do
    eval echo -ne '$s$pos\|$u'
    sleep 0.3
    eval echo -ne '$s$pos/$u'
    sleep 0.3
    eval echo -ne '$s$pos一$u'
    sleep 0.3
    eval echo -ne '$s$pos\\\\$u'
    sleep 0.3
    eval echo -ne '$s$pos\|$u'
    sleep 0.3
    eval echo -ne '$s$pos\$$u'
    sleep 0.3
done

[root@hello ~]|

As you were using, it works with last line in .bashrc

Thanks



回答2:

echo -ne '一\r'
#         ^
#         |
#         \--- problem

This character should be a hyphen, but is actually something called CJK Ideograph, First