How to echo only 1st line from 'curl' command output?

I'm trying to get only first line of the curl command output. (Sorry if this is confusing)

Let's say, for an instance, i run simply:

# curl http://localhost
<!-- This is the hidden line i want to grab. -->
<!DOCTYPE html>
<html>
<head>
..
..

What to do if i want the very first line of the output here, which is:

<!-- This is the hidden line i want to grab. -->

I've tried things like this, but no luck yet:

# curl http://localhost | head -n 1
# curl http://localhost | sed -n '1!p'

.. etc. All gives me rubbish outputs, like this:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0<!-- This is the hidden line i want to grab. -->
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (23) Failed writing body (173 != 1763)

It's not the output as expected as mentioned above:

<!-- This is the hidden line i want to grab. -->

Any experts here please =(


Solution 1:

This so called rubbish output is basically a progress meter during operation of downloading the data. You can basically ignore that, since it is by default going into standard error stream which is ignored, so only relevant part is printed out to standard output.

Here is the test:

$ curl http://example.com/ | head -n1 > example.html
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1270  100  1270    0     0   112k      0 --:--:-- --:--:-- --:--:--  124k
(23) Failed writing body
$ cat example.html 
<!doctype html>

If you still want to silence it, add -s parameter for quiet mode or redirect standard error stream into /dev/null, for example:

$ curl -s http://example.com/ 2> /dev/null | head -n1
<!doctype html>

Or using command substitution:

head -n1 <(curl -s http://example.com/ 2> /dev/null)