bash script, erase previous line?

前端 未结 6 1331
暗喜
暗喜 2021-01-31 10:31

In lots of Linux programs, like curl, wget, and anything with a progress meter, they have the bottom line constantly update, every certain amount of time. How do I do that in a

6条回答
  •  萌比男神i
    2021-01-31 11:22

    {
      for pc in $(seq 1 100); do
        echo -ne "$pc%\033[0K\r"
        usleep 100000
      done
      echo
    }
    

    The "\033[0K" will delete to the end of the line - in case your progress line gets shorter at some point, although this may not be necessary for your purposes.

    The "\r" will move the cursor to the beginning of the current line

    The -n on echo will prevent the cursor advancing to the next line

提交回复
热议问题