Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Shell that automatically trims spaces for no reason

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.9
  • bash 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading