Replacing variable values in html tags

Hi please help me with this .

I have a file test.txt with following content
$cat test.txt
<td>$test</td>
<h2>$test2</h2>

and I have a ksh with following content

$cat test.ksh

#!/bin/ksh
test=3
test2=4
while read line
do
echo $line
done < test.html

I am expecting the output as
<td>3</td>
<h2>4</h2>

but I get the out put as

<td>$test</td>
<h2>$test2</h2>

please let me know how can I get the expected output and where my logic is failing :slight_smile:

It doesn't work because shell doens't do that. It doesn't evaluate variables inside variables inside variables inside variables -- and that's mostly a good thing, you could tie yourself in knots with that.

I'd modify the file a little bit, into this:

cat <<EOF
<td>$test</td>
<h2>$test2</h2>
EOF

Then use it like

. test.txt

That will do it.

Hi Corona688,

Thanks for the reply and sorry to bother you again.

I am pretty new to this and I am afraid I didn't understand the how to use the file that is modified. Please explain. I have changed the content of file to

$cat test
cat <<EOF
<td>$test</td>
<h2>$test2</h2>
EOF

But I am not sure how to use this in the script to have the variable values replaced.

So you must write all lines manually..
shell writes to stdoutput what just read from your file..

# test=3;test2=4
# echo "<td>$test</td>"
<td>3</td>
# echo "<td>$test2</td>"
<td>4</td>

or maybe test with sed (also manually)

# test=3;test2=4;sed 's/$test\b/'$test'/;s/$test2/'$test2'/' test.html
<td>3</td>
<h2>4</h2>

regards
ygemici

I have just worked around the cat and EOF,got the desired out put. Thanks for your help.