The curl option –write-out is one of my personal favorites and offers users an excitingly powerful way to output information from a transfer. Over time, it has been extended to provide more and more features.
It was for example not that long ago we added the ability to output the content of specific headers with %headers{} to this option.
Now (shipping in the coming curl 8.3.0, merged in commit 1032f56efa) we take the next step and add yet another little nifty function to this option that makes it even more powerful and allows you to use it for more purposes better going forward.
It can now save the selected info to a specific file instead of just outputting to stdout or stderr. Or to multiple files. Or append to files. With %output{}
.
Examples
Write the used IP address of the remote host to a file named “remote.txt”:
curl -w "%output{remote.txt}%{remote_ip}" https://example.com
Get the same information, but append it to the remote.txt file:
curl -w "%output{>>remote.txt}%{remote_ip}" https://example.com
Output the HTTP response code from the HTTP server to stderr and then append it to the file “log.txt” as well:
curl -w "%{response_code}%output{>>log.txt}%{response_code}" https://example.com
Enjoy!
If >> signifies appending wouldn’t it be more consistent with shell syntax to use > for creating/replacing an existing file?
Perhaps, I just thought that it would be an extra character not really necessary…