This relies on widely available wget
, present almost everywhere, even on Alpine Linux.
wget --server-response --spider --quiet "${url}" 2>&1 | awk 'NR==1{print $2}'
The explanations are as follow :
--quiet
Turn off Wget's output.
Source - wget man pages
--spider
[ ... ] it will not download the pages, just check that they are there. [ ... ]
Source - wget man pages
--server-response
Print the headers sent by HTTP servers and responses sent by FTP servers.
Source - wget man pages
What they don't say about --server-response
is that those headers output are printed to standard error (sterr), thus the need to redirect to stdin.
The output sent to standard input, we can pipe it to awk
to extract the HTTP status code. That code is :
$2
) non-blank group of characters: {$2}
NR==1
And because we want to print it... {print $2}
.
wget --server-response --spider --quiet "${url}" 2>&1 | awk 'NR==1{print $2}'