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


View this message in rfc822 format

From: Paul Eggert <eggert <at> cs.ucla.edu>
To: Andi Kleen <andi <at> firstfloor.org>
Cc: Norihiro Tanaka <noritnk <at> kcn.ne.jp>, 41700 <at> debbugs.gnu.org
Subject: bug#41700: grep -v always exiting with 1 for empty file
Date: Thu, 4 Jun 2020 13:27:39 -0700
On 6/4/20 12:30 PM, Andi Kleen wrote:

> in your definition would 
> 
> echo -n foo > file
> 
> (so no newline, but non zero length)

No, the file has zero length:

$ echo -n >file
$ ls -l file
-rw-r--r-- 1 eggert eggert 0 Jun  4 13:24 file
> have one or zero lines?

Empty files have no lines.

On 6/4/20 1:13 PM, Eric Blake wrote:
> The most intuitive behavior is that grep behaves as if the file included the
> trailing newline

That's what grep does with files that end in a non-newline byte; such files are
also not text files so POSIX does not specify the behavior. But grep, like other
GNU tools, treats empty files as if they contain no lines; this matches most
people's intuition.




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.