Questions tagged [pipe]

A Unix pipe connects file descriptors of two processes. A pipe is created with the POSIX pipe() function declared in <unistd.h>. Shells provide pipe creation between processes using "|".

Pipes and data streams

Every command or program executed by the shell has 3 data streams associated with it:

  1. standard input (stdin, with a file descriptor of 0) – where commands get their input from (by default, keyboard input provided by the terminal).
  2. standard output (stdout, file descriptor 1) – where commands send their output (by default, the terminal display).
  3. standard error (stderr, file descriptor 2) – where commands send their error and warning messages (by default, the terminal display).

There are ways to connect streams between programs and files called piping and redirections.

Piping is a mechanism for sending data from one program to another using the "|" operator in most shells. The operator feeds the output from the program on the left as input to the program on the right.

Example:

$ cat two_columns
column1:cloth
column2:strawberries
column3:fish
column4:chocolate
column5:punch cards

$ cat two_columns | awk -F: '{print $1}'
column1
column2
column3
column4
column5

$ cat two_columns | awk -F: '{print "HAS: " $2}'
HAS: cloth
HAS: strawberries
HAS: fish
HAS: chocolate
HAS: punch cards

Further reading

Related tags

Useful links

1841 questions
123
votes
5 answers

In what order do piped commands run?

I've never really thought about how the shell actually executes piped commands. I've always been told that the "stdout of one program gets piped into the stdin of another," as a way of thinking about pipes. So naturally, I thought that in the case…
62
votes
4 answers

What are the advantages of using named pipe over unnamed pipe?

I was reviewing a set of interview questions that are asked from a unix admin; I found a topic called "named pipe". I googled the topic; to some extent I have been able to understand it :- named pipes || FIFO But still I feel that I am lacking the…
Ankit
  • 1,385
33
votes
3 answers

What makes a Unix process die with Broken pipe?

Here are some options I thought of, not sure which is the right one. There was an I/O error reading from the pipe. The process writing to the other end of the pipe died with a failure. All processes who could write to the pipe have closed it. The…
siamii
  • 557
33
votes
4 answers

Is there a way to pipe the output of one program into two other programs?

Sorry if this is a silly question but I'm trying to accomplish something like this but on one line: $ prog1 | prog2 $ prog1 | prog3 So, I basically want to execute prog1 and pipe the output to prog2 and prog3 separately (not a chained pipe). At…
longda
  • 431
24
votes
1 answer

How does a pipeline knows when to stop

Some programs can have an infinite runtime and produce infinite output. When used in a pipe with a command that finish, how does the pipeline knows when to stop ? For instance, take yes (infinite output) with head: yes | head -n 5 It will only…
Cyrbil
  • 389
  • 1
  • 4
  • 10
23
votes
4 answers

How to measure size of piped data?

I would like to do something like this: > grep pattern file.txt | size -h 16.4 MB or something equivalent to: > grep pattern file.txt > grepped.txt > ls -h grepped.txt 16.4 MB > rm grepped.txt (that would be a bit inconvenient, though) Is that…
Raffael
  • 941
15
votes
2 answers

What exactly is the difference between pipes and streams?

I read about pipes and streams and I’m still confused on how it’s implemented. A program is started and it reads data from “standard input” stream (stdin), which is where the keyboard sends data to. My question is, how is that different from a…
10
votes
3 answers

Why can't certain programs (like readlink) take input from a pipe?

I have a symbolic link to a script in my $PATH whose file I wanted to edit. I forgot the filepath, so I tried to do the following: $ which my_script_link | readlink I expected that to output the filepath, but instead it output > readlink: missing…
10
votes
2 answers

How can I tell if the pipe buffer is full?

I am piping output from one program into some Perl I wrote. This is a long running process , sometimes days, so I want to find out where my bottlenecks are and try to open them up. I want to know if data is being piped into my script faster than…
Big D
  • 103
  • 1
  • 5
7
votes
1 answer

Read until pipe is closed

I'm working on a homework assignment right now for an introduction to Operating Systems and am having quite a bit of fun, but confusion at the same time. I'm working on piping at the moment; my bit of code below here. Originally, my code looked like…
7
votes
2 answers

Are commands in a pipeline run simultaneously?

In a pipeline such as command1 | command2, are the commands run simultaneously (i.e. have the same lifetime), or command2 starts to run at the time command1 exits, or something else? My question arises when I heard that the commands' processes…
Tim
  • 101,790
4
votes
2 answers

Process each line of output from `ping` immediately in pipeline

I have a few examples of different ways of extracting timing information from ping -c 10 google.com. For some of these pipelines, a line of output is produces every so often, just like the output of ping. For others, the lines of output are emitted…
Greg Nisbet
  • 3,076
3
votes
2 answers

Printing to terminal while piping

% ping -c 3 stackexchange.com | rev >/tmp/rping saves the result of pinging and reversing. I would like to do this while at the same time printing the unreversed output of ping to the terminal. I would like to do this in real time, not all at…
Toothrot
  • 3,435
2
votes
1 answer

Flush data to file frequently for long running command?

I have a command that processes data slowly. The command processes lines from a file and writes the results to the output file data.txt: my_command > data.txt The issue I have is that I'd like to examine output lines in the data.txt file as they are…
turtle
  • 2,697
  • 5
  • 20
  • 17
2
votes
2 answers

Redirecting from right to left

From old habits, I'm always redirecting "from left to right", eg. cat file | bar foo | bar I've noticed you can redirect "from right to left". However, is it possible to do this for the second form (eg. from one program to another)? bar <…
forthrin
  • 2,289
1
2 3