How to update command line output?

On Linux, while stdout on the command line, I want to update a specific area, such as the output of apt-get :

 54% [Waiting for headers] [Waiting for headers] 211 kB/s 3s 

percent, kB / s and second will be updated every second.

If we use printf , we get some lines outputting them. I tried following printf :

 printf("\e[1;1H\e[2J"); 

But it clears all output.

My question is: how to update a specific area and maintain stability?

+7
source share
2 answers

Use carriage return. It will return the cursor to the beginning of the line from which you can overwrite what was in the line before. For example:

 printf("Hello, world!\rX"); 

Will display as:

 Xello, world! 

If you want it to be visible, make sure you are not just stdout :

 fflush(stdout); 
+7
source

In addition (useful \r and fflush tips above), if you want to get a full screen console, consider using ncurses . If you need an editable input line, it is useful to use the GNU readline library!

+3
source

All Articles