Skip to content

Instantly share code, notes, and snippets.

@timmc
Last active November 16, 2022 03:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timmc/1524e73039238ea4a94c106f8f71f90f to your computer and use it in GitHub Desktop.
Save timmc/1524e73039238ea4a94c106f8f71f90f to your computer and use it in GitHub Desktop.
Get HTTP status code and body in separate variables using curl
#!/bin/bash
all_output="$(curl -isS https://www.spidersge.org/)"
status=$(echo "$all_output" | grep -Po '\s[0-9]{3}\s' | head -n1 | tr -dc 0-9)
echo "Status is [$status]"
body="$(echo "$all_output" | sed -ne $'/^\r$/,$p' | tail -n+2)"
echo "Body is [$body]"
#!/bin/bash
all_out="$(curl -sS https://www.spidersge.org/ -w '%{http_code}')"
status="$(echo "$all_out" | tail -c4)"
echo "Status is [$status]"
body="$(echo "$all_out" | head -c-4)"
echo "Body is [$body]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment