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 fail-early stops at the first broken URL in a batch

Curl
~/tips/tip-curl-fail-early

curl -fsS --fail-early \

Make a multi URL curl command exit with an error as soon as one transfer fails.

Give curl several URLs and, by default, only the last one decides the exit code. A failed first download followed by a good second one returns 0, and your script carries on.

Add --fail-early and curl stops at the first failure and returns its error:

curl -fsS --fail-early \
  -O https://example.com/release/app.tar.gz \
  -O https://example.com/release/app.tar.gz.sha256
echo $?

If the first file returns a 404, curl prints the error, skips the second file and exits with 22. Without the option it would fetch the checksum file and exit with 0, and a step later you would be checking a file that does not exist.

The -f matters here. --fail-early reacts to transfer errors, and a 404 only counts as one when -f is set. The man page says the same: the option does not imply --fail.

I’d add it to any command or config file that downloads more than one thing a later step depends on. The flag is global, so it applies to every URL in the command, including ones after --next. It does not undo transfers that already finished, so files from earlier URLs stay on disk.

Further reading: curl man page

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

Link