There are many ways to save a multi line string in bash.
This is post just shows how to output a multiline string.

The Basics – Quoting Variables

From the bash(1) man page:

Also single quotes(e.g. ‘EOF’) or double quotes(e.g. “EOF”) can be used as stop tokens
The minus trims leading spaces or blanks. The quotes around the token prevent variable expansion inside document.

Examples

i. heredoc with variable expansion

ii. heredoc without variable expansion

The quoted “EOF” directive is determines whether or not variable name inside the multi line string will be expanded.

iii. heredoc without variable expansion – example 2

Escaping dollar inside a heredoc string is error prone and should be avoided!
So option ii. is the best

1. Set Multi Line Text to a String Variable

2. Set Multi Line Text to a String Variable

3. Set Multi Line Text to a String Variable

4. Set Multi Line Text to a String Variable

5. Set Multi Line Text to a String Variable

6. Save A Multi Line String to a Text File

Test File Contents

7. Save A Multi Line String to a Text File

this redirect does not work …

8. Save A Multi Line String to a Text File

9. Append a Multi Line String to a Text File

10. Save Multi Line String to a Text File

11. Save Multi Line String to a Text File

tee –help

References

1. Heredoc Quoting – Credit to Ignacio Vazquez-Abrams: http://serverfault.com/questions/399428/how-do-you-escape-characters-in-heredoc
2. eredoc Quoting – Credit to Dennis Williamson: http://stackoverflow.com/questions/3731513/how-do-you-type-a-tab-in-a-bash-here-document
3. http://serverfault.com/questions/72476/clean-way-to-write-complex-multi-line-string-to-a-variable
4. http://arstechnica.com/civis/viewtopic.php?p=21091503
5. http://superuser.com/questions/201829/sudo-permission-denied
6. http://stackoverflow.com/questions/4937792/using-variables-inside-a-bash-heredoc
7. http://stackoverflow.com/questions/2600783/how-does-the-vim-write-with-sudo-trick-work
8. http://www.unix.com/shell-programming-scripting/187477-variables-heredoc.html

About Gugulethu Ncube

IT enthusiast getting things done.