7

When I use sudo apt-get install <package-name> for installing any package, I observe the following:

Downloading some packages (78%)

This line once printed never changes its position but instead keeps on updating itself with percentage values. While other things are changing this line remains constant! How do they do it?

enter image description here

2
  • may be it has something to do with VDU memory.. direct read, write, save, restore, etc.
    – rusty
    Commented Jan 22, 2014 at 16:58
  • @null-pointer no need to trace your username in the screenshot when it's still on top of the window (username@hostname:~)... 8-) (Maybe you want to re-upload an edited version of your screenshot.)
    – mcantsin
    Commented Jan 22, 2014 at 23:43

2 Answers 2

12

You probably need to look at what echo -ne '\r' does.

Here is an example:

echo -ne 'Downloading 1%\r'
sleep 1

echo -ne 'Downloading 100%\r'
sleep 1

echo -ne '\n'
11
  • Great! It has exactly same working! Thanks a lot! Commented Jan 22, 2014 at 17:06
  • nice one for scripts... +1
    – rusty
    Commented Jan 22, 2014 at 17:09
  • 2
    \r is also known as "carriage return", for the record :)
    – Xavier J
    Commented Jan 22, 2014 at 17:14
  • Ya! I tried it in a C program and it worked! Commented Jan 22, 2014 at 17:17
  • 1
    You can also use \b to back up 1 character: echo -n "downloading: 001"; sleep 1; echo -e '\b\b\b100' Commented Jan 22, 2014 at 17:30
0

What Jobin said is correct, they do it with carriage return "\r". However, I think the OP was looking for a real world programming example.

Now, I'm not 100% familiar with the C/C++ syntax, but here's a working Python example:

Open a Terminal and type "python" to begin scripting.

Type the following code line for line, once you've type the last line hit the ENTER key twice and watch it work. Make sure after the "for i in range(11):" part, on the next lines you MUST hit the TAB key to indent in. It's proper Python formatting. If you don't hit TAB then you'll get an error.

import sys
import time
for i in range(11):
    time.sleep(1)
    i=i*10
    sys.stdout.write("\rDownload is %d%% complete!" %i)
    sys.stdout.flush()

So that's KIND OF how the terminal does what you asked about.

1
  • Thanks dude! I already tried this in C and it works, I have also mentioned this in the comments! Commented Jan 23, 2014 at 2:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .