Search code examples
shellwgetstderr

where is stderr of wget look so different when writing to screen than to a file


For example, when using wget

$ wget https://pypi.python.org/packages/source/F/Flask/Flask-0.10.1.tar.gz

The output looks like

--2016-03-05 20:01:58--  https://pypi.python.org/packages/source/F/Flask/Flask-0.10.1.tar.gz
Resolving pypi.python.org (pypi.python.org)... 199.27.74.223
Connecting to pypi.python.org (pypi.python.org)|199.27.74.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 544247 (531K) [application/octet-stream]
Saving to: ‘Flask-0.10.1.tar.gz’

100%[====================================================================================================================================================================================================>] 544,247     2.38MB/s   in 0.2s   

2016-03-05 20:01:59 (2.38 MB/s) - ‘Flask-0.10.1.tar.gz’ saved [544247/544247]

But when I redirect it to a log

$ wget https://pypi.python.org/packages/source/F/Flask/Flask-0.10.1.tar.gz &> tmp.log
$ cat tmp.log 
--2016-03-05 20:02:54--  https://pypi.python.org/packages/source/F/Flask/Flask-0.10.1.tar.gz
Resolving pypi.python.org (pypi.python.org)... 199.27.74.223
Connecting to pypi.python.org (pypi.python.org)|199.27.74.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 544247 (531K) [application/octet-stream]
Saving to: ‘Flask-0.10.1.tar.gz.3’

     0K .......... .......... .......... .......... ..........  9%  443K 1s
    50K .......... .......... .......... .......... .......... 18%  409K 1s
   100K .......... .......... .......... .......... .......... 28%  433K 1s
   150K .......... .......... .......... .......... .......... 37%  374K 1s
   200K .......... .......... .......... .......... .......... 47%  374K 1s
   250K .......... .......... .......... .......... .......... 56%  338K 1s
   300K .......... .......... .......... .......... .......... 65%  337K 0s
   350K .......... .......... .......... .......... .......... 75%  241K 0s
   400K .......... .......... .......... .......... .......... 84%  346K 0s
   450K .......... .......... .......... .......... .......... 94%  384K 0s
   500K .......... .......... .......... .                    100%  693K=1.4s

2016-03-05 20:02:55 (369 KB/s) - ‘Flask-0.10.1.tar.gz.3’ saved [544247/544247]

I am being very curious, wondering what happened when writing to the screen? How is the incremental appearance of those equal signs made possible, and where are they gone when redireced to a log?


Solution

  • wget calls isatty() on stderr to decide whether or not to display the incremental equals signs progress bar for the download. This is convenient for many reasons as we can send terminal control characters to back up and erase the line and rewrite it. This is done by a different mechanism when writing to a file, and not possible when writing to a pipe.