title: How to make a pipe loop in bash
body: <p>Assume that I have programs <code>P0</code>, <code>P1</code>, ...<code>P(n-1)</code> for some <code>n &gt; 0</code>. How can I easily redirect the output of program <code>Pi</code> to program <code>P(i+1 mod n)</code> for all <code>i</code> (<code>0 &lt;= i &lt; n</code>)?</p>
  
  <p>For example, let's say I have a program <code>square</code>, which repeatedly reads a number and than prints the square of that number, and a program <code>calc</code>, which sometimes prints a number after which it expects to be able to read the square of it. How do I connect these programs such that whenever <code>calc</code> prints a number, <code>square</code> squares it returns it to <code>calc</code>?</p>
  
  <p>Edit: I should probably clarify what I mean with "easily". The named pipe/fifo solution is one that indeed works (and I have used in the past), but it actually requires quite a bit of work to do properly if you compare it with using a bash pipe. (You need to get a not yet existing filename, make a pipe with that name, run the "pipe loop", clean up the named pipe.) Imagine you could no longer write <code>prog1 | prog2</code> and would always have to use named pipes to connect programs.</p>
  
  <p>I'm looking for something that is almost as easy as writing a "normal" pipe. For instance something like <code>{ prog1 | prog2 } &gt;&amp;0</code> would be great.</p>