Bash why quotes are important for variables; preserve newlines, line endings

#!/bin/bash
 
str=$(cat <<EOF
foo
bar
baz
EOF

)
 
echo $str
echo "$str"

Newlines are ignored when echoing the variable without surrounding quotes. Newlines are preserved when using echo and quoting the variable. The script produces the following output.

foo bar baz
foo
bar
baz
If you liked this posting, please feel free to leave me a comment below.
View this page on GitHub.
Posted .

Comments

Leave a Reply