brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: curl -w %header{} prints just the header value you ask for

Curl
~/tips/tip-curl-write-out-header

curl -s -o /dev/null -w '%header{cache-control}\n' https://example.com/

Read one response header in a script without grep, cut or carriage returns.

Most header checks end up as grep on curl -I output. Since curl 7.84 there is a cleaner way: the write-out variable %header{name} gives you one header value.

Name the header in the braces, in any case:

curl -s -o /dev/null -w '%header{cache-control}\n' https://example.com/
curl -s -o /dev/null -w '%{http_code} %header{content-type} %header{server}\n' https://example.com/

The first prints something like max-age=3600 and nothing else. The second combines the status code with two headers on one line. curl strips whitespace and the line ending from the value, so you can compare it in a script directly:

[ "$(curl -s -o /dev/null -w '%header{x-cache}' https://example.com/)" = HIT ] && echo cached

I find it handy to check what a CDN or reverse proxy sends after a config change, for headers like cache-control, age or x-cache.

The value comes from the most recent response. With -L that is the final page, not the redirect. A header that is missing gives an empty string, not an error, so test for that case. If a header appears more than once, look at %{header_json}, which lists every value as an array.

Further reading: curl man page, everything curl: write out

Going deeper: Guide: your Unraid cache is full and Docker stopped, how to find out why and fix it

More Curl tips curl --trace-ascii – https://example.com/ Retry-After: 120 curl -fsS --fail-early \ url = "https://example.com/data/latest.json" curl -4 -s -o /dev/null -w '%{remote_ip} %{http_code}\n' https://example.com/ All Curl tips (71) All topics

Link