I'm not sure about all the shells there are, but in Bash it's possible, though not with unnamed pipes. So not with the |
symbol. But if you create a named pipe:
mkfifo fifo
Then you can use it:
<fifo cat | cat >fifo &
Now the pipeline works in the background, but does nothing. But if you feed the pipe from outside the pipeline:
echo x >fifo
The pipeline will unblock and go on forever. Or until you drain the pipe:
cat fifo
The output will appear once:
x
To make this a little sophisticated, the pipeline may be this:
<fifo cat | xargs -I@ echo @x >fifo &
So it will add an x
to the output at each iteration. Of course it will, but only once the iterations start, meaning as soon as the pipe unblock, that is as soon as there's something to read. As previously, this can be started by hand:
echo x >fifo
And now take a look at what top
shows. There should be quite a lot of activity of both cat
and xargs
.
And the same as before, if you drain the pipeline, you should see a lot of x
s in the terminal, and the pipeline will block.
It would be a valid question, why does the pipeline get drained. Why is the cat
command committed in the terminal leaving nothing in the circuit. I don't know this.