[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

From: Eric Blake
Subject: bug#41700: grep -v always exiting with 1 for empty file
Date: Thu, 4 Jun 2020 15:13:44 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.8.0

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

reply via email to

[Prev in Thread] Current Thread [Next in Thread]