help-bash
[Top][All Lists]
Advanced

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

Re: question about read


From: Greg Wooledge
Subject: Re: question about read
Date: Wed, 26 Jul 2023 16:49:56 -0400

On Wed, Jul 26, 2023 at 10:22:08PM +0200, alex xmb ratchev wrote:
> there is problem on noneol end ? or the like ?
> i remember many discussion and my testing was it completly misses .. .. ?

If you read a line that doesn't end with a newline, read will return a
non-zero exit status, but will leave the content in the variable(s).

So, code that looks like this:

    while read -r line; do
        ...
    done < somefile

will "fail" to process the last line of the file, if that line is
incomplete (no newline).  This can be surprising.  The easiest workaround
(besides fixing your broken input file) is:

    while read -r line || [[ $line ]]; do
        ...
    done < somefile

That forces the loop body to be executed on the final partial line,
if there is one.

Did you have a different question, or is this what you were thinking of?



reply via email to

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