0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Check Website Status Quickly

Use curl -Is https://example.com | head -n 1 in a Bash script to check the website status line, or use curl -o /dev/null -s -w "%{http_code}" https://example.com to get just the HTTP status code.
📋

Examples

Inputhttps://www.google.com
OutputHTTP/2 200
Inputhttps://nonexistent.website
Outputcurl: (6) Could not resolve host: nonexistent.website
Inputhttps://httpstat.us/404
OutputHTTP/1.1 404 Not Found
🧠

How to Think About It

To check a website's status, the script sends a request to the website and reads the response's status line or code. It uses curl to fetch headers silently and extracts the first line or the HTTP status code to know if the site is reachable and what status it returns.
📐

Algorithm

1
Get the website URL as input
2
Send a HEAD request to the URL to fetch headers only
3
Extract the HTTP status line or code from the response
4
Print the status to show if the website is up or down
💻

Code

bash
#!/bin/bash

url="$1"

if [ -z "$url" ]; then
  echo "Usage: $0 <website_url>"
  exit 1
fi

status_code=$(curl -o /dev/null -s -w "%{http_code}" "$url")
echo "Status code for $url is: $status_code"
Output
Status code for https://www.google.com is: 200
🔍

Dry Run

Let's trace checking https://www.google.com through the code

1

Get input URL

url="https://www.google.com"

2

Run curl to get status code

status_code=$(curl -o /dev/null -s -w "%{http_code}" "https://www.google.com") status_code="200"

3

Print the status code

echo "Status code for https://www.google.com is: 200"

StepActionValue
1Input URLhttps://www.google.com
2curl status code200
3Output messageStatus code for https://www.google.com is: 200
💡

Why This Works

Step 1: Input URL

The script takes the website URL as input using $1, which is the first argument passed to the script.

Step 2: Fetch status code

It uses curl with options -o /dev/null to discard body, -s for silent mode, and -w "%{http_code}" to output only the HTTP status code.

Step 3: Display result

The script prints the status code with a message so you know if the website is reachable and what response it gave.

🔄

Alternative Approaches

Using curl to get full status line
bash
#!/bin/bash
url="$1"
if [ -z "$url" ]; then
  echo "Usage: $0 <website_url>"
  exit 1
fi
status_line=$(curl -Is "$url" | head -n 1)
echo "Status line for $url is: $status_line"
This shows the full HTTP status line like 'HTTP/1.1 200 OK' but requires parsing if you want just the code.
Using wget to check status
bash
#!/bin/bash
url="$1"
wget --spider -S "$url" 2>&1 | grep 'HTTP/' | head -n 1
Uses wget to send a HEAD request and prints the HTTP status line; useful if curl is not available.

Complexity: O(1) time, O(1) space

Time Complexity

The script runs a single network request which takes constant time relative to input size, so O(1).

Space Complexity

Uses only a few variables to store input and output, so O(1) space.

Which Approach is Fastest?

Using curl with -o /dev/null -s -w is fastest and cleanest; alternatives like wget add overhead and more output parsing.

ApproachTimeSpaceBest For
curl with -wO(1)O(1)Quick status code check
curl with -I and headO(1)O(1)Full status line info
wget --spiderO(1)O(1)When curl is unavailable
💡
Always check if the URL argument is provided to avoid errors in your script.
⚠️
Beginners often forget to handle empty input or do not suppress curl output, causing messy terminal output.