daniel
daniel

Reputation: 587

Handling pipes in C to create a shell

cat file.txt | grep hello

The way I see this is that cat file.txt is executed and whatever is passed to the stdout is passed as input in a pipe to grep hello. Then grep hello finds all the occurrences that match hello in the given input.

Here is my confusion: does grep hello write back to the pipe so the parent process outputs whatever is in stdout? Or does grep hello put its returned content in stdout? How does it work?

I am asking this question because I am writing a shell in C.

Upvotes: 0

Views: 67

Answers (1)

John Kugelman
John Kugelman

Reputation: 361615

cat's stdout is connected to grep's stdin. grep's stdout is connected to the default location, probably the terminal. grep doesn't feed its output back to cat -- the pipe is unidirectional.

Upvotes: 2

Related Questions