Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash script, erase previous line?

Tags:

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 bash script? All I can do now is just echo a new line, and that's not what I want because it builds up. I did come across something that mentioned "tput cup 0 0", but I tried it and it's kind of quirky. What's the best way?

like image 677
Matt Avatar asked May 02 '11 19:05

Matt


People also ask

How do you delete a line in a Bash script?

:g/^#/d - Remove all comments from a Bash script. The pattern ^# means each line beginning with # . :g/^$/d - Remove all blank lines. The pattern ^$ matches all empty lines.

How do you delete old lines in Linux?

Ctrl + A Ctrl + K - move the cursor at the beginning of the line and clear all the current line from the beginning to the end. You can then recall the cleared line with Ctrl + Y if you need.

How do I remove the last line in Unix?

txt (if it ends with EOF , or \n and then EOF ), the number of lines in new_file. txt may be the same (as file. txt ) after this command (this happens when there is no \n ) - in any case, the contents of the last line is deleted.

How do you delete a specific line in a file Linux?

To delete a line, we'll use the sed “d” command. Note that you have to declare which line to delete. Otherwise, sed will delete all the lines.


1 Answers

{   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

like image 81
linuts Avatar answered Oct 11 '22 08:10

linuts