Bash – Why is this bash command not echoing into a variable and what can I do to improve

bashscriptingunixwget

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 http://domain.tomonitor.com 2>&1; );

EDIT: Various solutions I've tried

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

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

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

EDIT2:

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:

3.887

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

3.910

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

I'm expecting lines like this:

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

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

Best Answer

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 http://domain.tomonitor.com; } 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).