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