Writing HTML with variables from an array or other means

Hello!

I wish to create a script that will automate the creation of HTML files...

for example, if my HTML starts out containing:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>$TITLE</title>
</head>

I want to put that into something that I can read and write later on. My idea was to put that into an array, then call it like so:

HTML_HEADER=(
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>$TITLE</title>
</head>
)

Then later:

TITLE=1Fish2Fish

for z in "${HTML_HEADER[@]}"
do
echo "z" >> OUTPUT.HTML
done

This doesn't work, as there are quotes and other characters I assume will be mis-interpreted by bash.

I want to be able to write the HTML lines without having to escape quotes and other characters and as my HTML changes, I want to make it simple for me to just drop in the modified HTML code into the array.

Please let me know if there is an easy way to go about this, or if what I am trying to accomplish does not make sense.

Thank you in advance!:eek:

Please use code tags for posting code fragments or data samples.

If awk is an option then you could do something like:

TITLE="1Fish2Fish"

awk -v T="$TITLE" '
        BEGIN {
                print "<html xmlns=\"http://www.w3.org/1999/xhtml\">"
                print "<head>"
                print "<title>" T "</title>"
                print "</head>"
        }
'

Thank you for the reply, but that's the same thing as doing echo for each line and escaping the quotes.. I am trying to find a way where you don't have to do all that. :confused:Also, noted about the code tags.

Few corrections:

TITLE="1Fish2Fish"

HTML_HEADER=(
"<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<title>${TITLE}</title>
</head>"
)

for z in "${HTML_HEADER[@]}"
do
        echo "$z" >> OUTPUT.HTML
done

Still escaping quotes... goal is to not have to modify the HTML content to do that.

---------- Post updated at 02:22 PM ---------- Previous update was at 02:21 PM ----------

... or is there a way I can do that in the 'for z in' ... statement so that quotes are automatically escaped? I don't want to have to manually escape quotes every time I edit the HTML - I will have a large number of lines so this is all about the convenience of being able to drop in new HTML whenever I want without filtering/escaping manually.

---------- Post updated at 02:47 PM ---------- Previous update was at 02:22 PM ----------

The below works for quotes- but the problem is that if you introduce an HTML 'tick mark' character ' then it breaks.

FISH_STICKS=('

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />


<title>$TITLE</title>
</head>
'
)

TITLE=TEST123

while IFS= read -r line
do
    echo "$line" | sed "s/\$TITLE/$TITLE/g"
done <<< "$FISH_STICKS"