linuxbashshellcurl

Curl "write out" value of specific header


I am currently writing a bash script and I'm using curl. What I want to do is get one specific header of a response.

Basically I want this command to work:

curl -I -w "%{etag}" "server/some/resource"

Unfortunately it seems as if the -w, --write-out option only has a set of variables it supports and can not print any header that is part of the response. Do I need to parse the curl output myself to get the ETag value or is there a way to make curl print the value of a specific header?

Obviously something like

curl -sSI "server/some/resource" | grep 'ETag:' | sed -r 's/.*"(.*)".*/\1/'

does the trick, but it would be nicer to have curl filter the header.


Solution

  • The variables specified for "-w" are not directly connected to the http header. So it looks like you have to "parse" them on your own:

    curl -I "server/some/resource" | grep -Fi etag | sed -r 's/.*"(.*)".*/\1/'