Basics

What is stdin — and why a loop over a server list only handles the first one

Illustration: a conveyor feeds slips into a machine's funnel, but a smaller machine has stuck its own pipe into the same funnel and swallows everything first

Here's a classic puzzle. You have a hosts file with three servers. You write a loop: for each line, ssh in and show the uptime.

while read h; do ssh "$h" uptime; done < hosts

The first server works. The loop ends. The second and third — silence, not a single error.

No magic involved: ssh quietly ate the rest of the file. It read it from the same place while reads from. That place is called stdin.

What stdin is, in plain words

stdin (standard input) is the input a program gets data through by default. If nobody told the program "read from a file", it reads from here.

Every program in the terminal has three pipes: one in and two out. The outputs are stdout and stderr: that's where it writes results and errors. stdin is the way in.

Picture a machine with a funnel on its side. Whatever goes into the funnel is what the machine processes. Usually the funnel faces your keyboard. But you can put it under anything.

Where stdin gets its data

Three options, and you've seen them all:

  • The keyboard. Run cat with no arguments and type. It repeats every line. To finish, press Ctrl+D — that's the "no more data" signal.
  • A file via <. sort < names.txt — the funnel is placed under a file. The program doesn't even know it's a file.
  • Another program via |. cat log.txt | grep ERROR — the first program's stdout flows straight into the second one's stdin. That's a pipe.

One more useful touch: many programs treat a dash - as "read from stdin". For example, curl -d @- https://... takes the request body from whatever you feed it.

Why the loop only handled one server

Back to the puzzle. done < hosts puts the file under the stdin of the whole loop. And every command inside gets the same funnel.

while read took the first line. Then ssh woke up and thought: "Oh, there's something on my input — I'll send it to the server." And slurped up everything left. On the next round read found nothing and ended the loop.

We tested the same thing with cat: a loop over three lines with cat > /dev/null inside printed only the first. Cut cat off the shared input — and all three are back.

How to fix it:

  1. ssh -n "$h" uptime — the -n flag tells ssh to leave stdin alone.
  2. The general trick for any command: command < /dev/null. /dev/null is an empty bottomless file. The program reads nothing and leaves your list alone.

Why CI and AI agents hang

The second stdin trouble looks different: everything just hangs. A minute, ten, an hour.

Usually a program asked a question and is waiting for an answer on stdin. npx create-next-app asks about TypeScript. apt install checks "Do you want to continue? [Y/n]". You'd press Enter. But CI or an AI agent has no keyboard — and nobody will ever answer.

Claude Code, Codex and GitHub Actions run commands with no live human on the input. Often with no terminal at all — that's the TTY story. So:

  • Look for a "don't ask" flag: --yes, -y, --no-input, --non-interactive.
  • No flag? Feed the answer yourself: yes | command answers "y" to everything.
  • Or close the input: command < /dev/null. The program sees end-of-input and either takes the defaults or fails honestly with an error. An error beats waiting forever.

When you ask an agent to install something, add to the prompt: "run commands non-interactively, with flags like --yes". It'll save you stuck sessions.

How to hand a program multi-line text

Sometimes you need to feed several lines at once — say, an SQL query. That's what a heredoc is for: you write the text right in the script and it goes to the command's stdin. Handier than creating a temp file.

How is stdin different from arguments?

Arguments are what you type after the command name: grep ERROR log.txt. They're short and known up front. stdin is a stream that can be huge and arrive bit by bit. Some programs prefer arguments, others prefer input, many understand both.

How do I tell a program is waiting for input?

The cursor blinks on an empty line and there's no terminal prompt. Try Ctrl+D: if the program was waiting for data, it gets "end of input" and either continues or exits.

Can my own script read stdin?

Yes. In bash it's read, in Python sys.stdin, in Node process.stdin. That way your script can sit in the middle of a pipe like a real Unix tool.

The terminal, pipes, agents — in KODiQ these are short lessons where each idea is explained with a live example.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →