Skip to main content

How to Print the HTTP Status Code with curl -w

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

A plain curl request prints the page it gets back and nothing else. The status code, the number that says whether the request worked, stays hidden. -w prints it for you, together with any other value you pick, such as the time the request took.

This guide prints status codes from a small test server on your own computer, with real output from curl 8.7.1 in zsh 5.9 and bash 3.2.

Syntax​

Syntax
curl -s -o /dev/null -w '<format>' <url>
PartWhat it means
-w '<format>'Short for --write-out. Prints <format> once the request is done
%{http_code}Inside the format: the status code of the response, such as 200 or 404
%{time_total}Inside the format: how many seconds the request took
\nInside the format: a line break
-o /dev/nullThrows the page away, so only the format is printed
-sSilent mode: no progress meter or error messages
<url>The address to request

curl's manual describes the option in one line: Make curl display information on stdout after a completed transfer. The format is plain text with variables in it: All variables are specified as %{variable_name} and to output a normal % you write them as %%. You can output a newline by using \n, a carriage return with \r and a tab space with \t.

The two variables this guide uses, in the manual's words:

  • http_code: The numerical response code that was found in the last retrieved HTTP(S) or FTP(s) transfer.
  • time_total: The total time, in seconds, that the full operation lasted.

The Example​

To try -w without touching a real website, you serve one page from your own computer with Python's built-in web server. The page index.html exists, so the server answers 200. missing.html doesn't, so it answers 404.

Prerequisites​

  • Python 3, for the test server. Check with python3 --version; this guide uses Python 3.15.2. To test an address of your own instead, skip Step 1.

Step 1: Start a Test Web Server​

Create a folder called site, and in it a file called index.html with this text:

site/index.html
<!doctype html>
<title>Hello</title>
<p>Hello from a local web server.</p>

Then, in the folder that holds site, start the server:

Terminal
python3 -m http.server 8047 --bind 127.0.0.1 --directory site
Output
Serving HTTP on 127.0.0.1 port 8047 (http://127.0.0.1:8047/) ...

--directory site is the folder it serves, --bind 127.0.0.1 and 8047 are the address and port it listens on. The server keeps running in this window, so open a second terminal window for the next steps.

Step 2: Print the Status After the Page​

Terminal
curl -s -w 'HTTP status: %{http_code}\n' http://127.0.0.1:8047/
Output
<!doctype html>
<title>Hello</title>
<p>Hello from a local web server.</p>
HTTP status: 200

curl printed the page as usual, and then the format: the text HTTP status: , then 200 in place of %{http_code}, then a line break from \n. The single quotes hand the format to curl exactly as typed, backslash included. The bash manual: Enclosing characters in single quotes (‘'’) preserves the literal value of each character within the quotes.

Step 3: Print Only the Status Code​

Most of the time you want the code alone, for example to check that an address answers. Throw the page away with -o /dev/null:

Terminal
curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8047/
curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8047/missing.html

macOS-style terminal window titled Terminal with two curl commands that use -s, -o /dev/null and -w '%http_code\n', one for the test server's home page and one for missing.html.

Output
200
404

The home page exists, so the answer was 200. missing.html doesn't, so the server answered 404, and curl printed it the same way. The manual's own advice for this: To suppress response bodies, you can redirect output to /dev/null. -s is silent mode, which the manual describes as Do not show progress meter or error messages.

Step 4: Add the Time the Request Took​

A format can hold several variables, with any text around them:

Terminal
curl -s -o /dev/null -w '%{http_code} in %{time_total} seconds\n' http://127.0.0.1:8047/
Output
200 in 0.000944 seconds

macOS-style terminal window titled Terminal showing a real zsh session: the status-only curl commands print 200 and 404, and the command with %time_total prints 200 in 0.000944 seconds.

The request took 0.000944 seconds, less than a millisecond, from a server on the same computer. bash printed the same codes, with its own time: 200 in 0.001102 seconds.

When you're done, go back to the server's window and press Ctrl+C:

Output
Serving HTTP on 127.0.0.1 port 8047 (http://127.0.0.1:8047/) ...
127.0.0.1 - - [03/Dec/2026 16:01:37] "GET / HTTP/1.1" 200 -
^C
Keyboard interrupt received, exiting.

The server logged each request with its status code, and stopped at Ctrl+C.

Common Mistakes​

MistakeWhat happensDo this instead
No \n at the end: -w '%{http_code}'zsh printed 200%, and bash put its next prompt straight after the code: 200$End the format with \n
No quotes around the format: -w %{http_code}\ncurl printed 200nPut the format in single quotes
A wrong variable name: -w '%{status_code}\n'curl: unknown --write-out variable: 'status_code'Use %{http_code}

The % after 200 isn't part of the output. The zsh manual explains it: When a partial line is preserved, by default you will see an inverse+bold character at the end of the partial line: a ‘%’ for a normal user or a ‘#’ for root. The n comes from the shell. The bash manual: A non-quoted backslash ‘\’ is the Bash escape character. It preserves the literal value of the next character that follows, removing any special meaning it has, with the exception of newline. So without quotes, curl got a plain n.

FAQ​

What does 000 mean?​

That no response came back at all. With nothing listening on port 8048, curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8048/ printed 000, and echo $? printed curl's exit code, 7, which the manual lists as Failed to connect to host. -w still printed a code, because This output is done independently of if the file transfer was successful or not.

How do I keep the status code in a variable?​

Run curl inside $(...), which puts what it prints into the variable:

Terminal
code=$(curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:8047/missing.html)
echo "Status: $code"
Output
Status: 404

The format has no \n this time, because the code goes into the variable rather than onto the screen.

What else can -w print?​

Many values. -w '%{http_code} %{content_type} %{size_download} bytes\n' printed 200 text/html 75 bytes for the test page. The manual describes content_type as The Content-Type of the requested document, if there was any. and size_download as The total amount of bytes that were downloaded.

Conclusion​

curl -s -o /dev/null -w '%{http_code}\n' printed 200 for the page that exists and 404 for the missing one, and adding %{time_total} printed how long the request took. Keep the format in single quotes and end it with \n. A 000 means no answer came back at all.

Additional Resources​