bug-bash
[Top][All Lists]
Advanced

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

Re: bash "while do echo" can't function correctly


From: Greg Wooledge
Subject: Re: bash "while do echo" can't function correctly
Date: Wed, 13 Apr 2016 10:33:26 -0400
User-agent: Mutt/1.4.2.3i

On Wed, Apr 13, 2016 at 03:28:02PM +0100, Stephane Chazelas wrote:
> 2016-04-13 08:55:16 -0400, Greg Wooledge:
> [...]
> > > And if you want to keep eventual spurious characters after the
> > > last NL character in the file:
> > > 
> > > while IFS= read -r line; do printf '%s\n' "$line"; done < test.txt
> > > [ -z "$line" ] || printf %s "$line"
> > 
> > Another way to write that is:
> > 
> > while IFS= read -r line || [[ $line ]]; do ... done < test.txt
> [...]
> 
> Except that it would add an extra newline character (which may
> be desired as well).

I'm assuming you're using the input in some way, not just dumping it
back out.  If all you want is to dump the input back out to stdout,
the correct way to write that is:

cat

In a loop that does something nontrivial to the input, treating each
line of input as a data item to be processed after removing any trailing
newlines, the way I wrote it is preferred because it lets you avoid
writing the big complicated loop body twice.  (Or moving the loop body
to a function, which you then call from two different places.)



reply via email to

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