You can implement this by overwriting a line. Use
to go back to the beginning of the line without writing
to the terminal.
Write
when you're done to advance the line.
Use echo -ne
to:
- not print
and
- to recognize escape sequences like
.
Here's a demo:
echo -ne '##### (33%)
'
sleep 1
echo -ne '############# (66%)
'
sleep 1
echo -ne '####################### (100%)
'
echo -ne '
'
In a comment below, puk mentions this "fails" if you start with a long line and then want to write a short line: In this case, you'll need to overwrite the length of the long line (e.g., with spaces).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…