[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: bash "while do echo" can't function correctly
From: |
Stephane Chazelas |
Subject: |
Re: bash "while do echo" can't function correctly |
Date: |
Wed, 13 Apr 2016 13:43:42 +0100 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
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
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"
For details, see:
https://unix.stackexchange.com/questions/209123/understand-ifs-read-r-line
https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo
https://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice
--
Stephane