From owner-freebsd-hackers@FreeBSD.ORG Sat Jun 5 20:48:46 2010 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 87F2C1065672 for ; Sat, 5 Jun 2010 20:48:46 +0000 (UTC) (envelope-from bakul@bitblocks.com) Received: from mail.bitblocks.com (mail.bitblocks.com [64.142.15.60]) by mx1.freebsd.org (Postfix) with ESMTP id 48C9A8FC08 for ; Sat, 5 Jun 2010 20:48:46 +0000 (UTC) Received: from bitblocks.com (localhost.bitblocks.com [127.0.0.1]) by mail.bitblocks.com (Postfix) with ESMTP id 6E56B5B52; Sat, 5 Jun 2010 13:48:45 -0700 (PDT) To: Doug Barton In-reply-to: Your message of "Sat, 05 Jun 2010 13:32:08 PDT." <4C0AB448.2040104@FreeBSD.org> References: <20100605201242.C79345B52@mail.bitblocks.com> <4C0AB448.2040104@FreeBSD.org> Comments: In-reply-to Doug Barton message dated "Sat, 05 Jun 2010 13:32:08 -0700." Date: Sat, 05 Jun 2010 13:48:45 -0700 From: Bakul Shah Message-Id: <20100605204845.6E56B5B52@mail.bitblocks.com> Cc: freebsd-hackers@freebsd.org Subject: Re: head behaviour X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 05 Jun 2010 20:48:46 -0000 On Sat, 05 Jun 2010 13:32:08 PDT Doug Barton wrote: > On 06/05/10 13:12, Bakul Shah wrote: > > Consider: > > > > $ yes | cat -n | (read a; echo $a; head -1) > > 1 y > > 2 y > > > > $ yes | cat -n | (head -1; read a; echo $a) > > 1 y > > 456 y > > It's not at all clear to me what you are trying to accomplish here. If > what you want is to read only the first line of the output of yes, then > what you'd want to do is: > > yes | cat -n | head -1 | (read a; echo $a) > 1 y > > > As you can see, head reads far more than it should. This is > > fine most of the time but often it results in surprising > > output: > > > > # print ps header and all lines with sh in it > > $ ps|(head -1; grep sh) > > PID TT STAT TIME COMMAND > > I don't understand why you think this would work. There is no input to > the grep command. The only reason it exits at all is that you are > executing in a subshell. > > > # print first and last two lines > > $ look xa | (head -2; tail -2) > > xanthaline > > xanthamic > > Same problem here. There is no input to the tail command. In general this is not true. Without running the following can you guess its output? $ look '' | (head -2; head -2) Will it produce A a or A a aa aal or A a sive abrastol or something else? Yes, we can always find a work around for a given case but the issue is that head buffers up more than it needs to.