Heredoc sounds more convenient for this purpose. It is used to send multiple commands to a command interpreter program like ex or cat
cat << EndOfMessage
This is line 1.
This is line 2.
Line 3.
EndOfMessage
The string after << indicates where to stop.
To send these lines to a file, use:
cat > $FILE <<- EOM
Line 1.
Line 2.
EOM
You could also store these lines to a variable:
read -r -d '' VAR << EOM
This is line 1.
This is line 2.
Line 3.
EOM
This stores the lines to the variable named VAR.
When printing, remember the quotes around the variable otherwise you won't see the newline characters.
echo "$VAR"
Even better, you can use indentation to make it stand out more in your code. This time just add a - after << to stop the tabs from appearing.
read -r -d '' VAR <<- EOM
This is line 1.
This is line 2.
Line 3.
EOM
But then you must use tabs, not spaces, for indentation in your code.
text="this is line one\nthis is line two\nthis is line three"in the same one line..? (without any enter) - Yohanes Khosiawan 许先汉\non each line, you have already hit newline to move to the new line - Mark Setchell\n.So why you put next line in new line? Simplytext="this is line one\nthis is line two\nthis is line three"- Jayesh Bhoi\nat the end of each line causes the output to all run together on a single line. - Jonathan Hartley"$text"in the echo line is crucial. Without them, none of the newlines (both literal and '\n') work. With them, they all do. - Jonathan Hartley