Subject: Re: cat(1) question: multiple "-"s
To: Chavdar Ivanov <ci4ic4@gmail.com>
From: Steven M. Bellovin <smb@cs.columbia.edu>
List: netbsd-users
Date: 04/18/2006 11:19:50
On Tue, 18 Apr 2006 16:01:29 +0100, "Chavdar Ivanov" <ci4ic4@gmail.com>
wrote:

> On 18/04/06, Hubert Feyrer <hubert@feyrer.de> wrote:
> > On Tue, 18 Apr 2006, Chavdar Ivanov wrote:
> > > root@loan8>   cat - - - < /tmp/1 < /tmp/2 </tmp/3
> > >                                             /tmp
> > > 11111
> > > 22222
> > > 33333
> >
> > Wow. How does that work?
> >
> > I'm familiar with the concepts of stdio, stdin and eof, but
> > how does cat/zsh know when to attach the next file to the stdin file
> > descriptor?
> 
> >From the zshmisc man page:
> ....
> If the user tries to open a file descriptor for reading more than once,
>        the  shell opens the file descriptor as a pipe to a process that copies
>        all the specified inputs to its output in the order specified,  similar
>        to cat, provided the MULTIOS option is set.  Thus
> 
>               sort <foo <fubar

I confess that it's still very unclear to me.  First, processes don't
normally open stdin; second, if they did, only a driver would know; the
shell is long since out of the picture.  

As best I can tell from trying it, it's concatenating all of the input
files into a single stream.  This means that

	cat - - c <a <b

is the same as 

	cat a b c

but

	cat - b - <a <c

will act like

	cat a c b

I tried to verify this, but when I tried it I ran into some weird bugs.

		--Steven M. Bellovin, http://www.cs.columbia.edu/~smb