Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got a bash script I'm working to improve, and have put on a great fix thanks to Dennis Williamson. Unfortunately, one of the lines no longer echoes into a variable I can manipulate, rather dumps the output directly. I'll be good to go if I fix this.

Why is this bash command not echoing into the $result variable and what can I do to improve?

  result=$( time wget -q --output-document=/tmp/wget.$$.html 2>&1; );

EDIT: Various solutions I've tried

  result=$( { time (/usr/local/bin/wget -q --output-document=/tmp/wget.$$.html --header="Host:" http://$host) } &2>1 );

  result=$( { time (/usr/local/bin/wget -q --output-document=/tmp/wget.$$.html --header="Host:" http://$host) } );

  result=$( ( time (/usr/local/bin/wget -q --output-document=/tmp/wget.$$.html --header="Host:" http://$host) ) );


I'm echoing out a line like this:

  echo "$date, $host, $result"

Date and host are currently fine. $result is not.

I'm getting lines like this:


Tue Feb 15 08:39:53 PST 2011,,


Tue Feb 15 08:39:57 PST 2011,,

I'm expecting lines like this:

Tue Feb 15 08:39:53 PST 2011,, 3.887

Tue Feb 15 08:39:57 PST 2011,, 3.910

share|improve this question
No, look carefully at the line in my answer (and the one in my answer to your other question). (The first line in your edit is truncated, by the way.) Please be specific regarding what you want to capture and where. – Dennis Williamson Feb 15 '11 at 16:47
Saw and fixed the truncated line. Checking on others. Thank you again for your time and patience. – editor Feb 15 '11 at 16:49
You are correct. I am not sure what I missed in your answer but it is certainly the right one. – editor Feb 15 '11 at 16:51
You corrected the truncated line, but I suspect that dollar sign at the end is an artifact of nano. – Dennis Williamson Feb 15 '11 at 17:08
up vote 2 down vote accepted

You need curly braces around the whole command inside the command substitution but before the redirection:

result=$( { time wget -q --output-document=/tmp/wget.$$.html; } 2>&1; );

That captures the output of time. If you want to capture the error output of wget (you're already saving its regular output to a file), you'll need to do something a little different and it depends on what exactly you want to do. It may involve extra file descriptors or simply using the redirection I showed in my answer to your other question (instead of using --output-document).

share|improve this answer
Thanks (again!) Dennis. I started with that per your last note but somehow lost the curly brace while trying out different solutions. For me, it doesn't yet seem to work with or without. – editor Feb 15 '11 at 16:37
@editor: See my edited answer. Please let me know specifically what's happening - error messages, unexpected behavior, etc. Do you want the output of time in the variable? What do you want to do if wget outputs an error? – Dennis Williamson Feb 15 '11 at 16:40
It works for me! Which bash version are you using? Check it with "bash --version". Is $results empty? What do you expect to get there? The output of the time command? I do get it. – rems Feb 15 '11 at 16:42
@ editor: Please be specific. You say it "spits directly into the output buffer". It spits what? The output of time? The output of wget? The error output of wget? – Dennis Williamson Feb 15 '11 at 17:06
Try var=$( { time sleep 1; } 2>&1 ); echo "[$var]". Is the output of time inside the square brackets or outside? – Dennis Williamson Feb 15 '11 at 17:11

I think this should go over to stack overflow but did you try this?

result=$( time (wget -q --output-document=/tmp/wget.$$.html 2>&1;) );
share|improve this answer
Just tried to no avail. I thought the command line-esque stuff came under the Serverfault umbrella but if someone needs to migrate I understand. – editor Feb 15 '11 at 16:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.