How to delete and replace last line in the terminal using bash?

后端 未结 7 1198

I want to implement a progress bar showing elapsed seconds in bash. For this, I need to erase the last line shown on the screen (command \"clear\" erases all the screen, but

相关标签:
7条回答
  • 2020-11-30 18:35

    Derek Veit's answer works well as long as the line length never exceeds the terminal width. If this is not the case, the following code will prevent junk output:

    before the line is written for the first time, do

    tput sc
    

    which saves the current cursor position. Now whenever you want to print your line, use

    tput rc
    tput ed
    echo "your stuff here"
    

    to first return to the saved cursor position, then clear the screen from cursor to bottom, and finally write the output.

    0 讨论(0)
提交回复
热议问题