Glossary

stdin

Standard input (stdin) is the default stream from which a program reads its input. It corresponds to file descriptor 0. When you run a command at an interactive shell, stdin is typically connected to the terminal: you type, the program reads. When you redirect with < file, stdin instead reads from the file. When a command appears on the right of a pipe, its stdin is connected to the previous command's stdout.

wc -l < /etc/passwd          # read from a file
ps aux | grep python          # grep's stdin is ps's stdout
sort <<< "apple\\nbanana\\ncherry"   # here-string (bash)

Programs that support stdin input include cat, grep, sort, wc, sed, awk, jq, less, head, tail, and many more. A common idiom is to let - denote stdin as a filename, so commands like tar -x -f - read a tar archive from stdin.

Detecting whether stdin is a terminal or a pipe lets programs adapt: cat and less behave differently, and isatty(0) is the C-level check that underlies it. In a shell script, [[ -t 0 ]] plays the same role.

Related terms: stdout, stderr, File Descriptor, Pipe, Redirection

Discussed in:

Also defined in: Textbook of Linux