You don't write anything to foo because time sends its output to stderr . The problem is that the wget command also sends most of its output to stderr . To separate the two streams (and throw the output from wget ), you will need to use subshell :
TIMEFORMAT=%R; foo=$( time ( wget http://www.example.com 2>/dev/null 1>&2 ) 2>&1 ) echo $foo
Here is an explanation of what is happening ...
The inside of this command is:
( wget http://www.example.com 2>/dev/null 1>&2 )
Sends both stderr and stdout to /dev/null , essentially discarding them.
Outside:
foo=$( time ( ... ) 2>&1 )
Sends stderr from the time command to the same place where stdout sent, so that it can be captured using the replacement ( $() ).
Update:
If you want to become really smart, you can pass the output of wget to stderr by manipulating file descriptors as follows:
foo=$( time ( wget http://www.example.com 2>&1 ) 3>&1 1>&2 2>&3 )
Lee netherton
source share