# Replacing variable values in html tags

**URL:** <https://community.unix.com/t/replacing-variable-values-in-html-tags/296094>\
**Category:** Shell Programming and Scripting\
**Created:** [September 26, 2011, 11:12am UTC](https://community.unix.com/t/replacing-variable-values-in-html-tags/296094 "2011-09-26T11:12:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![panduandpavan](https://community.unix.com/letter_avatar/panduandpavan/32/5_5575768a8748004e209b776fc1b2916d.png) [@panduandpavan](https://community.unix.com/u/panduandpavan)\
**Post date:** [September 26, 2011, 11:12am UTC](https://community.unix.com/t/replacing-variable-values-in-html-tags/296094/1 "2011-09-26T11:12:42Z")

</div>

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

```nohighlight
$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 🙂

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [September 26, 2011, 11:20am UTC](https://community.unix.com/t/replacing-variable-values-in-html-tags/296094/2 "2011-09-26T11:20:24Z")

</div>

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:

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

```

Then use it like

```nohighlight
. test.txt

```

That will do it.

---

<div class="post-metadata">

**Author:** ![panduandpavan](https://community.unix.com/letter_avatar/panduandpavan/32/5_5575768a8748004e209b776fc1b2916d.png) [@panduandpavan](https://community.unix.com/u/panduandpavan)\
**Post date:** [September 27, 2011, 12:48am UTC](https://community.unix.com/t/replacing-variable-values-in-html-tags/296094/3 "2011-09-27T00:48:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ygemici](https://community.unix.com/letter_avatar/ygemici/32/5_5575768a8748004e209b776fc1b2916d.png) [@ygemici](https://community.unix.com/u/ygemici)\
**Post date:** [September 27, 2011, 5:39am UTC](https://community.unix.com/t/replacing-variable-values-in-html-tags/296094/4 "2011-09-27T05:39:32Z")

</div>

> [@panduandpavan](#):
>
> 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..

```plaintext
# 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)

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

```

regards  
ygemici

---

<div class="post-metadata">

**Author:** ![panduandpavan](https://community.unix.com/letter_avatar/panduandpavan/32/5_5575768a8748004e209b776fc1b2916d.png) [@panduandpavan](https://community.unix.com/u/panduandpavan)\
**Post date:** [September 28, 2011, 2:24am UTC](https://community.unix.com/t/replacing-variable-values-in-html-tags/296094/5 "2011-09-28T02:24:36Z")

</div>

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