head behaviour

Doug Barton dougb at FreeBSD.org
Sat Jun 5 20:32:11 UTC 2010


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.


Doug

-- 

	... and that's just a little bit of history repeating.
			-- Propellerheads

	Improve the effectiveness of your Internet presence with
	a domain name makeover!    http://SupersetSolutions.com/



More information about the freebsd-hackers mailing list