When I read "line1\nline2\nline3" into a string, like this:
read string
line1\nline2\nline3
Then echo the string and direct the output to a file:
echo $string > text.txt
The txt file now contains:
line1nline2nline3
How could I make it so that the file contains:
line1
line2
line2
?
Thanks.
Just put
$string
in double-quotes:The problem here is that
\n
does not mean line feed. It's just unnecessarily escaping the value ofn
.To do what you want, you should.
You can do 1. with
read -r
and 2. withecho -e
:You need add double-quotes.
Example:
With double-quotes:
Saving: