Yes, this is related to Getting curl to output HTTP status code? but unfortunately not the same.

In a script I would like to run:

curl -qSfsw %{http_code} URL

where the -f option ensures that the exit code is non-zero to signal an error. On success I want to get the (textual) output from the fetched file, whereas otherwise I want to use the error code.


  • Due to race conditions I must not use more than a single HTTP request
  • I cannot use a temporary file for storage of the content

How can I still split the HTTP return code from the actual output?

Pseudo code:

fetch URL
if http-error then
  print http-error-code
  print http-body # <- but without the HTTP status code

1 Answer 1


There is no need to use a temporary file. The following bash script snippet send a single request and prints the exit code of curl and the HTTP status code, or the HTTP status code and response, as appropriate.

# get output, append HTTP status code in separate line, discard error message
OUT=$( curl -qSfsw '\n%{http_code}' http://superuser.com ) 2>/dev/null

# get exit code

if [[ $RET -ne 0 ]] ; then
    # if error exit code, print exit code
    echo "Error $RET"

    # print HTTP error
    echo "HTTP Error: $(echo "$OUT" | tail -n1 )"
    # otherwise print last line of output, i.e. HTTP status code
    echo "Success, HTTP status is:"
    echo "$OUT" | tail -n1

    # and print all but the last line, i.e. the regular response
    echo "Response is:"
    echo "$OUT" | head -n-1

head -n-1 (print all but the last line) requires GNU, doesn't work on BSD/OS X.

  • For OS X, substitute... head -n-1 ...with... sed \$d Nov 8, 2016 at 20:10
  • the only drawback is this fails to save the body of the actual error response. i ended up forgoing the -f flag and checking the response status code manually Jun 5, 2020 at 19:32

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.