I'm trying this:
TIMEFORMAT=%R;
foo=$(time wget http://www.mysite.com)
echo $foo
and when I execute I see the number I want in the output but not in variable foo (echo $foo print nothing).
Why is that?
I'm trying this:
TIMEFORMAT=%R;
foo=$(time wget http://www.mysite.com)
echo $foo
and when I execute I see the number I want in the output but not in variable foo (echo $foo print nothing).
Why is that?
You are not capturing anything in
foo
becausetime
sends its output onstderr
. The trouble is that thewget
command also sends most of its output onstderr
. To split the two streams (and throw away the output fromwget
) you will need to use a subshell:Here is an explanation of what's going on...
The inner part of this command:
Sends both
stderr
andstdout
to/dev/null
, essentially throwing them away.The outer part:
Sends
stderr
from thetime
command to the same place thatstdout
is sent so that it may be captured by the command substitution ($()
).Update:
If you wanted to get really clever, you can have the output of
wget
passed through tostderr
by juggling the file descriptors like this: