Subject: bug#41700: grep -v always exiting with 1 for empty file

From: Eric Blake <[email-masked]>
Date: Thu, 4 Jun 2020 15:13:44 -0500

On 6/4/20 2:30 PM, Andi Kleen wrote:
> Just out of curiosity, in your definition would
> 
> echo -n foo > file

[side point: echo -n is not portable, printf is better]

> 
> (so no newline, but non zero length)
> 
> have one or zero lines?

Neither. Per POSIX, a non-empty file that does not end in newline is not 
a text file, and therefore POSIX says that tools like grep that are only 
documented to work on text files have unspecified behavior on such input.

The most intuitive behavior is that grep behaves as if the file included 
the trailing newline, in which case the output (if there is a match) 
actually displays a newline that was not present in the input, but there 
have also been historical grep that behave as if the file ended at the 
last newline, and ignore the trailing garbage even if it would have 
matched were a trailing newline present.

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org