Clean way to write complex multi-line string to a variable
Solution 1:
This will put your text into your variable without needing to escape the quotes. It will also handle unbalanced quotes (apostrophes, i.e. '
). Putting quotes around the sentinel (EOF) prevents the text from undergoing parameter expansion. The -d''
causes it to read multiple lines (ignore newlines). read
is a Bash built-in so it doesn't require calling an external command such as cat
.
IFS='' read -r -d '' String <<"EOF"
<?xml version="1.0" encoding='UTF-8'?>
<painting>
<img src="madonna.jpg" alt='Foligno Madonna, by Raphael'/>
<caption>This is Raphael's "Foligno" Madonna, painted in
<date>1511</date>-<date>1512</date>.</caption>
</painting>
EOF
Solution 2:
You've been almost there. Either you use cat for the assembly of your string or you quote the whole string (in which case you'd have to escape the quotes inside your string):
#!/bin/sh
VAR1=$(cat <<EOF
<?xml version="1.0" encoding='UTF-8'?>
<painting>
<img src="madonna.jpg" alt='Foligno Madonna, by Raphael'/>
<caption>This is Raphael's "Foligno" Madonna, painted in
<date>1511</date>-<date>1512</date>.</caption>
</painting>
EOF
)
VAR2="<?xml version=\"1.0\" encoding='UTF-8'?>
<painting>
<img src=\"madonna.jpg\" alt='Foligno Madonna, by Raphael'/>
<caption>This is Raphael's \"Foligno\" Madonna, painted in
<date>1511</date>-<date>1512</date>.</caption>
</painting>"
echo "${VAR1}"
echo "${VAR2}"
Solution 3:
#!/bin/sh
VAR1=`cat <<EOF
<?xml version="1.0" encoding='UTF-8'?>
<painting>
<img src="madonna.jpg" alt='Foligno Madonna, by Raphael'/>
<caption>This is Raphael's "Foligno" Madonna, painted in
<date>1511</date>-<date>1512</date>.</caption>
</painting>
EOF
`
echo "VAR1: ${VAR1}"
This should work fine within Bourne shell environment
Solution 4:
Yet another way to do the same...
I like to use variables and special <<-
who drop tabulation at begin of each lines to permit script indentation:
#!/bin/bash
mapfile Pattern <<-eof
<?xml version="1.0" encoding='UTF-8'?>
<painting>
<img src="%s" alt='%s'/>
<caption>%s, painted in
<date>%s</date>-<date>%s</date>.</caption>
</painting>
eof
while IFS=";" read file alt caption start end ;do
printf "${Pattern[*]}" "$file" "$alt" "$caption" "$start" "$end"
done <<-eof
madonna.jpg;Foligno Madonna, by Raphael;This is Raphael's "Foligno" Madonna;1511;1512
eof
warning: there is no blank space before eof
but only tabulation.
<?xml version="1.0" encoding='UTF-8'?>
<painting>
<img src="madonna.jpg" alt='Foligno Madonna, by Raphael'/>
<caption>This is Raphael's "Foligno" Madonna, painted in
<date>1511</date>-<date>1512</date>.</caption>
</painting>
Some explanations:
- mapfile read entire here document in an array.
- the syntaxe
"${Pattern[*]}"
do cast this array into a string. - I use
IFS=";"
because there is no;
in required strings - The syntaxe
while IFS=";" read file ...
preventIFS
to be modified for the rest of the script. In this, onlyread
do use the modifiedIFS
. - no fork.
Solution 5:
There are too many corner cases in many of the other answers.
To be absolutely sure there are no issues with spaces, tabs, IFS etc., a better approach is to use the "heredoc" construct, but encode the contents of the heredoc using uuencode
as explained here:
https://stackoverflow.com/questions/6896025/#11379627.