Here Document Bash Variable Assignment

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

Ifany characters inwordare quoted,
the delimiter isthe result of quote removal on word,
andthe lines inthe here-document are notexpanded.
cat>myfile.txt<<EOF
thisfilehas$variable$names$inside
#print out the content of myfile.txt
#this file has    
inside="expanded variables"
cat>myfile.txt<<EOF
thisfilehas$variable$names$inside
#print out the content of myfile.txt
#this file has ONE TWO expanded variables
cat>myfile.txt<<"EOF"
thisfilehas$variable$dollar$name$inside
#print out the content of myfile.txt
#this file has $variable $dollar $name $inside
cat>myfile.txt<<EOF
thisfilehas$variable\$dollar\$name\$inside
#print out the content of myfile.txt
#this file has $variable $dollar $name $inside
read-d''stringvar<<-"_EOF_"
all the leading dollars inthe$variable$nameare$retained
# all the leading dollars in the $variable $name are $retained
read-d''help<<-"_EOF_"
  usage:up[--level<n>|-n<levels>][--help][--version]
  Report bugs to:
  up home page:
VARIABLE1="<?xml version=\"1.0\"encoding='UTF-8'?>
  <img src="a-vs-b.jpg"/>
  <caption>Thus isafuture post on Multi Line Stringsinbash
  <date>1511</date>-<date>1512</date>.</caption>
VARIABLE2=$(cat<<EOF
<?xml version="1.0"encoding='UTF-8'?>
  <img src="a-vs-b.jpg"/>
  <caption>Thus isafuture post on Multi Line Stringsinbash
  <date>1511</date>-<date>1512</date>.</caption>
VARABLE3=`cat<<EOF
<?xml version="1.0"encoding='UTF-8'?>
  <img src="a-vs-b.jpg"/>
  <caption>Thus isafuture post on Multi Line Stringsinbash
  <date>1511</date>-<date>1512</date>.</caption>
cat>heredocfile.txt<<_EOF_
# and then, change your echo statement to include the '-e' option
# which will turn on escape sequence processing:
echo-e$USAGE>&2
sudo cat>/aaaa.txt<<_EOF_
# sudo and >>: permission denied
sudo tee/aaa.txt<<EOF
  echo"Hello World 20314"
sudo tee-a  /aaa.txt<<EOF
echo"This Line is appended"
sudo sh-c"cat > /aaa.txt"<<"EOT"
thistext gets saved assudo-$10-ten dollars...
#this text gets saved as sudo - $10 - ten dollars ...
cat<<"EOF"|sudo tee/aaa.txt
Usage:tee[OPTION]...[FILE]...
Copy standard input toeachFILE,andalso tostandard output.
  -a,--append              append tothe given FILEs,donotoverwrite
  -i,--ignore-interrupts   ignore interrupt signals
      --help     display thishelp andexit
      --version  output version information andexit
IfaFILEis-,copy again tostandard output.
Report teebugs tobug-coreutils@gnu.org
GNU coreutils home page:<http://www.gnu.org/software/coreutils/>
General help using GNU software:<http://www.gnu.org/gethelp/>
Forcomplete documentation,run:info coreutils'tee invocation'
Posted in OS, ubuntu, unix.

The construct introduces a shell here document that is treated as if it were a double-quoted string as it is read, so variables expand immediately as they are read by the shell. You would see this clearly if you enable a shell feature that tells you about undefined variables. I'll use that feature in the BASH shell here:

To stop the shell from expanding variables in the here document as it is being read, you can either single-quote the tag or backslash every use of every variable:

Note that the types of quotes used around the variable inside the here document have nothing to do with whether or not the variable is expanded by the shell as the here document is being read.

p.s. Always double-quote your variable expansions so that they don't trigger unintended GLOB file name matches.

p.p.s. Turn on undefined-variable checking in you shells, to catch your typing errors. This would have clued you in to your problem.

p.p.p.s. Don't use C shells for scripting. They have many bugs and inconsistencies

0 thoughts on “Here Document Bash Variable Assignment”

    -->

Leave a Comment

Your email address will not be published. Required fields are marked *