1

I want the download speed and total time in a curl operation using curl -w. If I run

curl example.com -w '%{speed_download} %{time_total}'

The stdout is

<!doctype html> <html> <head> <title>Example Domain</title> <meta charset="utf-8" /> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style type="text/css"> body { background-color: #f0f0f2; margin: 0; padding: 0; font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; } div { width: 600px; margin: 5em auto; padding: 2em; background-color: #fdfdff; border-radius: 0.5em; box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02); } a:link, a:visited { color: #38488f; text-decoration: none; } @media (max-width: 700px) { div { margin: 0 auto; width: auto; } } </style> </head> <body> <div> <h1>Example Domain</h1> <p>This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.</p> <p><a href="https://www.iana.org/domains/example">More information...</a></p> </div> </body> </html> 2861.000 0.439478

The speed_download and time_total are appended to the stdout. So I can extract the curl response that would be had I not used the -w option as follows:

CURL_RESPONSE=$(curl example.com -w '%{speed_download} %{time_total}')
echo $CURL_RESPONSE | grep -oP ".*(?= ([0-9]+\.[0-9]+ [0-9]+\.[0-9]+$))"

and similarly to get the speed_download and time_total

echo $CURL_RESPONSE | grep -oP "([0-9]+\.[0-9]+ [0-9]+\.[0-9]+$)"

I want to avoid using this approach and redirect the output from -w '%{speed_download} %{time_total}' to a different file descriptor, say STDERR and keep STDOUT without these. In this documentation https://www.mit.edu/afs.new/sipb/user/ssen/src/curl-7.11.1/docs/curl.html it says:

-w/--write-out

Defines what to display after a completed and successful operation. The format is a string that may contain plain text mixed with any number of variables. The string can be specified as "string", to get read from a particular file you specify it "@filename" and to tell curl to read the format from stdin you write "@-".

We can read the format for writing the string from a file but is there a way to control where the displayed content is redirected to separately from the regular STDOUT from the curl had -w not been used?

1 Answer 1

2

Accoriding to the manual (man curl):

-w, --write-out

.....

All variables are specified as %{variable_name} and to output a normal % you just write them as %%. You can output a newline by using \n, a carriage return with \r and a tab space with \t. The output will be written to standard output, but this can be switched to standard error by using %{stderr}.

So you can use the next curl command to get speed_download and time_total:

curl example.com --silent -w '%{stderr} %{speed_download} %{time_total}' 1> /dev/null

If you want to get the another output (html) you can redirect file descriptor 1 or stdout to a file:

curl example.com --silent -w '%{stderr} %{speed_download} %{time_total}' 1> somefile

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .