http server – in bash!?!

A discussion came up on one of the LUGs this weekend (Victoria’s VLUG).

Craig had a situation with a router with low specs that he wanted to retrieve some info from. The idea of a simple http server in bash came up.

I thought, “Nah, can’t be done.” I was wrong. He found a Github repo of one (using nc).

That got me to thinking, what would ChatGPT do? Turns out, some very elegant code:

write a simple web server in bash


Writing a simple web server in Bash can be a fun exercise, but keep in mind that it might not be suitable for production use due to limitations and potential security risks. Below is a very basic example of a web server implemented in Bash:

ChatGPT 3.5

# Define the port on which the server will listen

# Function to handle incoming requests
handle_request() {
    # Read the HTTP request from the client
    read -r request

    # Extract the request method and path
    method=$(echo "$request" | awk '{print $1}')
    path=$(echo "$request" | awk '{print $2}')

    # Serve a simple response
    if [ "$method" = "GET" ]; then
        if [ "$path" = "/" ]; then
            echo -e "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<html><body><h1>Hello, World!</h1></body></html>" > fifo
            echo -e "HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n\r\n404 Not Found" > fifo
        echo -e "HTTP/1.1 501 Not Implemented\r\nContent-Type: text/plain\r\n\r\n501 Not Implemented" > fifo

# Main loop to listen for incoming connections
main() {
    echo "Starting server on port $PORT..."
    mkfifo fifo
    trap "rm -f fifo" EXIT

    # Continuously listen for connections
    while true; do
        # Wait for a client to connect
        cat fifo | nc -l -p "$PORT" > >(handle_request)


Save the above code into a file, for example, and make it executable with chmod +x Then you can run it with ./

This script creates a basic HTTP server that listens on port 8080. It can handle GET requests for the root path (“/”) and returns a simple “Hello, World!” HTML page. All other paths will result in a “404 Not Found” response. Additionally, it returns a “501 Not Implemented” response for any request method other than GET.

Please note that this is a very basic implementation and lacks many features found in real-world web servers, such as concurrency handling, error checking, security measures, etc. It’s more of a proof of concept rather than something suitable for production use.

ChatGPT 3.5

The main issue with the code is that the code outputs to the terminal, not across the network.
Adding a simple pipe to fifo on the three echo statements solved that.
I do like that it used port 8080 so root privileges aren’t required to bind to it.

A very impressive script – better than 99% of the bash scripts I encounter in the wild, and better than approximately 100% of the scripts I write.

Folks are still, “I don’t trust AI code” while trusting StackOverflow or Github, which I find weird – all those sources need roughly equal scrutiny. ChatGPT won’t intentionally put an obfuscated bitcoin miner in my code.
And, I got my answer faster than I could perform a search, evaluate the responses, and tune them for any special requirement.

Another comment from SFLUG – “Google the code to see where it was stolen from!”
Betrays a complete misunderstanding of how these language models work; as though they produce a collage of what they’ve seen from elsewhere, ignoring the ability to synthesize entirely new content (sometimes those are known as “hallucinations”).

This is another scary-good example of AI code, IMHO.

By admin

Linux admin, JS / React / Python / Perl developer.

Leave a comment

Your email address will not be published. Required fields are marked *