Linux - Standard Streams
In Linux, standard streams are structurally fixed to specific file descriptors (FDs). When you chain commands using a pipe (|), the operating system redirects these file descriptors to point to an anonymous kernel pipe.
The Fixed File Descriptors
The numerical positions for the three standard streams are strictly reserved and never change:
0=stdin(Standard Input)1=stdout(Standard Output)2=stderr(Standard Error)
Piped Command Example
To see how Linux routes these file descriptors through a pipe, run a command sequence that stays open:
sleep 100 | grep "search_term"
Inspecting the Pipe via /proc: While the command above runs, open a second terminal window to inspect its state.
Step A: Find the Process IDs (PIDs)
Locate the PIDs for both running applications:
ps aux | grep -E "sleep|grep"
Note: For the examples below, assume sleep is PID 1234 and grep is PID 5678.
Step B: Check the First Command (Pipe Out)
List the file descriptors for the writing process (sleep):
ls -l /proc/1234/fd/
Expected Output:
lr-x------ 1 user user 64 May 18 14:10 0 -> /dev/pts/0
l-wx------ 1 user user 64 May 18 14:10 1 -> pipe:[99999]
l-wx------ 1 user user 64 May 18 14:10 2 -> /dev/pts/0
0and2still connect to your interactive terminal (/dev/pts/0).1(stdout) points to an anonymouspipe:[99999].
Step C: Check the Second Command (Pipe In)
List the file descriptors for the reading process (grep):
ls -l /proc/5678/fd/
Expected Output:
lr-x------ 1 user user 64 May 18 14:10 0 -> pipe:[99999]
l-wx------ 1 user user 64 May 18 14:10 1 -> /dev/pts/0
l-wx------ 1 user user 64 May 18 14:10 2 -> /dev/pts/0
0(stdin) points to the exact samepipe:[99999]ID.1and2route directly back to your terminal.
The matching inode number inside the brackets (99999) confirms that the operating system has physically wired the output of the first process into the input of the second.