Programs also have a stream to read from. This is standard input, stream 0.
| Number | Stream | Goes to or comes from, by default |
|---|---|---|
| 0 | Standard input (stdin) | The keyboard |
| 1 | Standard output (stdout) | The terminal |
| 2 | Standard error (stderr) | The terminal |
No file name: read standard input
cat, grep, wc, head, tail and sort read their files when you give file names. When you give no file name, they read standard input.
If standard input is the keyboard, the program waits for you to type. On a real server, push Ctrl+D to end the input, or Ctrl+C to stop the program.
< : a file as standard input
< connects a file to standard input:
learner@tidepool:~$ wc -l orders.txt
12 orders.txt
learner@tidepool:~$ wc -l < orders.txt
12The count is the same. With <, the shell opens the file and gives its text to wc. wc never sees the name, so it writes only the number. Use this when you need the number alone, for example to save it in a report.
The next chapter connects the standard output of one program to the standard input of another.