head behaviour

Bakul Shah bakul at bitblocks.com
Sat Jun 5 21:37:32 UTC 2010


On Sat, 05 Jun 2010 14:02:16 PDT Doug Barton <dougb at FreeBSD.org>  wrote:
> On 06/05/10 13:48, Bakul Shah wrote:
> > Without running the following can you guess its output?
> >
> > $ look '' | (head -2; head -2)
> 
> Again, it's not clear to me what you expect is going to happen with the 
> second 'head -2' there. I agree that the actual output of your example 
> is wacky and unexpected, but what I'm trying to get you say is what YOU 
> think should happen. The examples that you pasted in your previous post 
> did not and could not do what you said you wanted them to do, so I don't 
> quite understand what the bug is.

There is no bug per se. What I am saying that it would be
*less surprising* if

    $ look '' | (head -2; head -2)

behaved the same as

    $ look '' | head -4

[And yes, I would use head -4 if I wanted four lines but the
example was to illustrate the issue that head buffers more
than it needs to].

It would be less surprising and more useful if

    $ ps | (head -1; grep ssh)

showed

    PID  TT  STAT      TIME COMMAND
    <all line with ssh in it>

The change in head behaviour I am suggesting wouldn't break
anything that already works but make it more useful for what
you call 'wacky commands lines'!

> Put more simply, if you generate wacky command lines you should not be 
> surprised when they produce wacky results. :)

I didn't realize that use of ;(|) constitutes wackiness :-)
They are simply exercising the power of shell!

We have used these commands for so long that we take them for
granted and we learn to avoid their use in such ways.  When
Rob Warnock first mentioned this, my initial reaction was the
same as yours. But thinking more about it, I felt head can be
made more useful with this change and felt it was worth
bringing it to people's attention. But we can let it rest.


More information about the freebsd-hackers mailing list