Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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 figure curl would do the job. I wrote in a script:


function test {
    res=`curl -I $1 | grep HTTP/1.1 | awk {'print $2'}`
    if [ $res -ne 200 ]
        echo "Error $res on $1"

test mysite.com
test google.com

The problem here is no matter what I do I can't get it to stop printing the below to stdout:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

I want a cronjob to run this script and if it writes such a message then every time I run it I'll get an email because something has been printed to stdout in cron, even though the site may be fine.

How do I get the status code without getting junk into stdout? This code works except the bonus junk to the stdout preventing me from using it.

share|improve this question
up vote 8 down vote accepted
          Silent or quiet mode. Don't show progress meter 
          or error messages.  Makes Curl mute.

So your res should look like

res=`curl -s -I $1 | grep HTTP/1.1 | awk {'print $2'}`

Result is Error 301 on google.com, for example.

share|improve this answer
Thank you! this irratated me so much. (and they should mention this under the "progress meter" in the man page) – acidzombie24 Dec 9 '11 at 13:22

you want

res=$( curl -w %{http_code} -s --output /dev/null $1)

That will give you the HTTP_STATUS code as the only output.

share|improve this answer

I'd do it like this to ensure i get redirected to the final host and get the response from it:

res=($(curl -sLI "${1}" | grep '^HTTP/1' | tail -1))
((res[1] == 200)) || echo ${res[2]}
share|improve this answer

Use this as your condition...

res=`curl -s --head <URL> | head -n 1 | grep -c HTTP/1.1 200 OK`

if [ $res -eq 1 ]
share|improve this answer

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.