How do I overwrite multiple lines in a shell script?

后端 未结 1 507
离开以前
离开以前 2021-02-06 14:00

I want to write multiple lines over and over to the terminal. Something like

echo \"One Line\"
echo \"Two Lines\"
echo \"\\r\\b\\rThree Lines\"
echo \"Four Lines         


        
1条回答
  •  心在旅途
    2021-02-06 14:24

    I found a solution for this one that took a little digging and I'm still not entirely sure on how this one works. However, it seems the program tput will allow you to get special characters for clearing lines and position the cursor. Specifically, tput el will clear to the beginning of the current line (instead of just repositioning the cursor). Conveniently, tput cuu1 will move the cursor up one line. So if in your bash script you declare variables like:

    UPLINE=$(tput cuu1)
    ERASELINE=$(tput el)
    

    You could then write a script like so:

    UPLINE=$(tput cuu1)
    ERASELINE=$(tput el)
    echo "One Line"
    echo "Two Lines"
    echo "$UPLINE$ERASELINE$UPLINE$ERASELINE\c"
    echo "Three Lines"
    echo "Four Lines"
    

    and you'll get the desired output.

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