Reputation: 6552
I am complete bash newbie.
I want to use the bash >>
operator to append some html to the end of a file. I would like to construct said HTML with three concatenated sections: an opening html tag, a variable defined by bash earlier, and a closing html tag.
Something like:
echo <div id="myid"> $myBashVariable </div> >> file.html
I am not sure what syntax is needed to escape the various characters needed for the HTML markup... <
,>
,/
,"
.
How can I make this work?
Upvotes: 1
Views: 1881
Reputation: 123570
Save yourself the trouble and use a here document:
#!/bin/sh
var="world"
cat >> file.html << EOF
<html>
<head>
<title>Hello $var</title>
</head>
<body>
<div id="whatever">Hello $var, and welcome to my page.</div>
</body>
</html>
EOF
Note that characters in $var
will not be HTML escaped, so if var='<script>alert(1)</script>'
you will get a JS popup.
Upvotes: 4
Reputation:
You need to escape with \
On windows,
echo "<div id=\"myid\">%myBashVariable%</div>" >> file.html
On Linux,
echo \<div\ id=\"myid\"\>$myBashVariable\</div\> >> file.html
Upvotes: 1
Reputation: 339
\ is the escape character that you have to add before the special characters to escape the special meaning of those.
Try this.
echo \<div id=\"myid\"\> $myBashVariable \</div\> >> file.html
Upvotes: 2