Introduction
I was trying to do some instructions on a file line by line with:
while IFS= read -r line; do
...
done < file
When I noticed that there was a problem with trailing whitespaces (for example: " a " => "a") that were automatically deleted, which is a real problem for me.
I searched in the documentation and didn’t find any mention of that. And there is the same problem with printf.
Minimal example:
touch example # Create a file
echo " exa mple " >> example # Add some text
cat example # exa mple
echo $(cat example) # exa mple
rm example # Delete the file
In this example, I don’t understand why echo $(cat example) doesn’t have some trailing whitespaces.
And this "problem" is also here with:
while IFS= read -r line; do
echo $line # exa mple
done < example
Version:
Tested with:
zsh v5.9bash v5.2.2
>Solution :
IFS= read -r line < file is correctly reading unmodified lines with leading and trailing spaces. You can confirm this by printing the variable using declare -p line.
But after you read the lines correctly, you are mangling them during your print commands. Both echo $(cat example) and echo $line have unquoted expansions, which cause the shell to word-split your lines.
Quote them to resolve the problem:
echo "$(cat example)"
echo "$line"
By the way, https://shellcheck.net/ is excellent for spotting and explaining errors like these.