Subject: Re: Likely bug in grep for file without an ending newline

From: Jim Meyering <[email-masked]>
Date: Fri, 7 Mar 2014 16:51:35 -0800

On Fri, Mar 7, 2014 at 2:38 PM, Abramo Bagnara <[email-masked]> wrote:
>
> $ echo -n test > file
> $ fgrep e file
> test
> $ fgrep -v q file
> test
> $ cat file
> test$
>
> My expectation would be that file is not altered by grep if all lines
> matches pattern.

Thank you for the report.
Grep is specified to operate on so-called "lines" which are defined
to end with a newline, so the moment you give grep an input that
does not consist of lines, you're in "unspecified" territory.  I.e., what
grep does with that input is unspecified by POSIX. We have
chosen to make it append the newline in that case.

> Is current grep behavior the expected one?

It is documented in "info grep":

  If the final byte of an input file is not a newline, `grep' silently
supplies one.