I haven’t tested this on a 500 code, but it works on others like 200, 302 and 404.
response=$(curl --write-out '%{http_code}' --silent --output /dev/null servername)
Note, format provided for –write-out should be quoted.
As suggested by @ibai, add --head
to make a HEAD only request. This will save time when the retrieval is successful since the page contents won’t be transmitted.