GNU bug report logs - #41700
grep -v always exiting with 1 for empty file

Previous Next

Package: grep;

Reported by: Andi Kleen <andi <at> firstfloor.org>

Date: Thu, 4 Jun 2020 04:33:01 UTC

Severity: normal

Tags: notabug

Done: Paul Eggert <eggert <at> cs.ucla.edu>

Bug is archived. No further changes may be made.

Full log


Message #23 received at 41700 <at> debbugs.gnu.org (full text, mbox):

From: Eric Blake <eblake <at> redhat.com>
To: Andi Kleen <andi <at> firstfloor.org>, Paul Eggert <eggert <at> cs.ucla.edu>
Cc: 41700 <at> debbugs.gnu.org
Subject: Re: bug#41700: grep -v always exiting with 1 for empty file
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





This bug report was last modified 4 years and 239 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.