[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 08:55:16 -0400 |
User-agent: |
Mutt/1.4.2.3i |
On Wed, Apr 13, 2016 at 01:43:42PM +0100, Stephane Chazelas wrote:
> 2016-04-13 08:10:15 +0200, Geir Hauge:
> [...]
> > while read -r line; do echo "$line"; done < test.txt
> >
> > though printf should be preferred over echo:
> >
> > while read -r line; do printf '%s\n' "$line"; done < test.txt
> [...]
>
> Actually, you also need to empty $IFS
>
> while IFS= read -r line; do printf '%s\n' "$line"; done < test.txt
That will preserve every full line of input (except for the NUL bytes),
including leading and trailing spaces.
If you actually *want* the leading and trailing IFS whitespace characters
to be trimmed (the default behavior of read), then do it Geir's way. In
many cases, that's desired.
> 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