From owner-freebsd-hackers@FreeBSD.ORG Sun Jun 6 14:35:20 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 8EB291065678; Sun, 6 Jun 2010 14:35:20 +0000 (UTC) (envelope-from des@des.no) Received: from smtp.des.no (smtp.des.no [194.63.250.102]) by mx1.freebsd.org (Postfix) with ESMTP id 4BB898FC19; Sun, 6 Jun 2010 14:35:20 +0000 (UTC) Received: from ds4.des.no (des.no [84.49.246.2]) by smtp.des.no (Postfix) with ESMTP id 3C8CA1FFC34; Sun, 6 Jun 2010 14:35:19 +0000 (UTC) Received: by ds4.des.no (Postfix, from userid 1001) id B1DB0844C6; Sun, 6 Jun 2010 16:33:09 +0200 (CEST) From: =?utf-8?Q?Dag-Erling_Sm=C3=B8rgrav?= To: Doug Barton References: <20100605201242.C79345B52@mail.bitblocks.com> <4C0AB448.2040104@FreeBSD.org> Date: Sun, 06 Jun 2010 16:33:09 +0200 In-Reply-To: <4C0AB448.2040104@FreeBSD.org> (Doug Barton's message of "Sat, 05 Jun 2010 13:32:08 -0700") Message-ID: <86r5kk6xju.fsf@ds4.des.no> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.95 (berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable 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: Sun, 06 Jun 2010 14:35:20 -0000 Doug Barton writes: > Bakul Shah writes: > > $ 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. The output from ps is piped to the subshell, so all processes within that subshell get their stdin from that output. Bakul's error is in assuming that head will *consume* only the first line of input, whereas the only guarantee actually given is that it will *print* only the first line of input. This particular one-liner can be implemented reliably as follows: % ps | (read header; echo $header; grep sh) PID TT STAT TIME COMMAND 3415 0 Ss 0:00.31 -zsh (zsh) 3476 0 S+ 0:00.00 -zsh (zsh) because read is a shell built-in, and the shell guarantees that it will not consume more input than necessary. DES --=20 Dag-Erling Sm=C3=B8rgrav - des@des.no