|
This is undefined behavior: while `head -1` will only output a single line, it may read more. It happens to work on GNU head when stdin is seekable file, because GNU head specifically rewinds the stream before exiting: $ (strace -e read,write,lseek head -1 > /dev/null; cat -) < file
...
read(0, "hello\nworld\n", 8192) = 12
lseek(0, -6, SEEK_CUR) = 6 # <-- here
write(1, "hello\n", 6) = 6
+++ exited with 0 +++
If not for that explicit `lseek`, `head -1` would have skipped the entire 8k buffer.As far as I know, this is exclusive to GNU cat. Neither Busybox nor OSX cat will do this, and will therefore throw away an entire buffer instead of just the first line. You can try it out: (busybox head -1 > /dev/null; cat -) < file |