Stdin Basics in Linux
In Linux systems, standard input (stdin) is a fundamental concept in file I/O operations. It represents the default input stream where programs receive data, typically from the keyboard or through input redirection.
Key Characteristics of Stdin
Stdin Attribute |
Description |
File Descriptor |
0 |
Default Source |
Keyboard |
Programmable |
Yes |
Stream Type |
Input stream |
graph LR
A[Keyboard Input] --> B[Stdin Stream]
C[File Input] --> B
D[Pipe Input] --> B
When you type directly into a terminal, the input is captured through stdin:
$ read user_input
Hello, LabEx!
Linux allows redirecting stdin from files using the <
operator:
$ wc -l < example.txt
Pipes (|
) can also redirect stdin between commands:
$ cat file.txt | grep "pattern"
Stdin in Programming
In C programming, stdin is represented by the stdin
pointer:
#include <stdio.h>
int main() {
char buffer[100];
fgets(buffer, sizeof(buffer), stdin);
printf("Input received: %s", buffer);
return 0;
}
Understanding stdin is crucial for effective Linux system programming and command-line interactions.