stdin (Standard In)
100%

Text-Fu · Lesson 2

stdin (Standard In)

Learn how programs read standard input and how Bash connects that stream to a file.

Standard input, abbreviated stdin, is the stream a program normally reads for incoming data. In an interactive terminal, the shell usually connects stdin to your terminal input, so a program can read what you type.

Standard Input and File Descriptor 0

By convention, the three standard streams use these file descriptor numbers:

  • 0: standard input (stdin)
  • 1: standard output (stdout)
  • 2: standard error (stderr)

A program can choose whether and how to use these streams. A command designed to read stdin often waits for terminal input when no file operand or other input source is supplied.

Which file descriptor conventionally represents standard input?

Redirecting a File into stdin

The < operator tells Bash to open a file for reading and connect it to the command's stdin:

$ cat < peanuts.txt
Hello World

The shell handles < peanuts.txt; cat simply reads file descriptor 0. The pathname is not passed to cat as a normal file operand.

If the input file does not exist or cannot be opened, the shell reports the redirection error and does not start the command with that input.

Which command makes sort read its standard input from names.txt?

File Operand versus Input Redirection

Some commands accept either a filename operand or stdin, but the results can differ slightly. For example:

$ wc -l peanuts.txt
1 peanuts.txt
$ wc -l < peanuts.txt
1

Both forms count lines in the same data. In the first form, wc knows the filename because it received it as an argument. In the second, it only receives a stream on stdin, so it has no filename to print.

Why does wc -l < peanuts.txt normally omit peanuts.txt from its output?

Combining Input and Output Redirection

One command line can redirect more than one stream:

$ cat < peanuts.txt > banana.txt

The shell performs two independent connections:

  1. < peanuts.txt opens peanuts.txt as cat's stdin.
  2. > banana.txt creates or truncates banana.txt and connects it to cat's stdout.

cat reads bytes from stdin and writes them to stdout, so banana.txt receives the source content. For ordinary file copying, cp peanuts.txt banana.txt communicates the intent more directly; this example is about stream connections.

In cat < input.txt > output.txt, which file supplies stdin and which receives stdout?

To practice input and output redirection, try these hands-on labs:

  1. Redirecting Input and Output in Linux - Practice controlling data flow from commands by manipulating standard output (stdout), standard error (stderr), and standard input (stdin) using operators like >, >>, 2>, and the tee command.
  2. Data Stream Redirection - Learn the art of Linux stream redirection. Manipulate standard input, output, and error streams, combine outputs, and utilize /dev/null for advanced file operations.

Lesson complete

You finished stdin (Standard In)

You can now connect a command's standard input to a file through the shell.

  • Recognize stdin as file descriptor 0.

  • Redirect a readable file with <.

  • Distinguish a filename operand from redirected input.

  • Combine stdin and stdout redirections deliberately.

Keep your learning progress

Create a free account to save this lesson and continue learning on any device.

Create a free account
Next Lesson
Back to Text-Fu