I have a bash-script which calls curl using a proxy. Today there is a problem with the download location.

Calling curl ends up with:

curl: (18) transfer closed with 7512708716 bytes remaining to read

Although this error occurs, the return code remains 200, so that my script "thinks" that everything was fine.

How can I say curl to give another return code than the http-200?

  • By return code do you mean the exit code seen in echo $?? My curl says it exits 18 in this case. The http header is one of the first lines received and cannot change from 200 if there is a later error. – meuh May 3 '16 at 10:55
  • Thank you, this was the mistake. First I used the following syntax: rc=$( curl ...) This returns the http code 200, not the real return code from curl. Then I changed my code to: httprc=$( curl ...); rc=$?; This gave me both return codes. Thank you! – schulle877 May 9 '16 at 6:28

curl --fail does part of what you want:

from man curl:

-f, --fail

(HTTP) Fail silently (no output at all) on server errors. This is mostly done to better enable scripts etc to better deal with failed attempts. In normal cases when an HTTP server fails to deliver a document, it returns an HTML document stating so (which often also describes why and more). This flag will prevent curl from outputting that and return error 22.

This method is not fail-safe and there are occasions where non-successful response codes will slip through, especially when authentication is involved (response codes 401 and 407).

But it blocks output to the screen.


The mistake was:

rc=$( curl ... )

This gave me the http code back because I filled $rc with the stdout of curl. I have to fill rc with $?afterwards.

After changing my code to:

httprc=$( curl ...)

I got both "return" codes back. Thanks to meuh!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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