1

I want create simple xml file using bash script ( not with dom )

but when I set value in the line then echo print the $lorem word and not the val

lorem=LOL

echo '<root><foo a="b">$lorem</foo><bar value="ipsum" /></root>' >> $MY_XML

I also try this

echo '<root><foo a="b">\$lorem</foo><bar value="ipsum" /></root>' >> $MY_XML

echo '<root><foo a="b">"$lorem"</foo><bar value="ipsum" /></root>' >> $MY_XML

echo '<root><foo a="b">\"$lorem\"</foo><bar value="ipsum" /></root>' >> $MY_XML

but all these print the exactly the line and not the val

please advice how to print the val $lorem ? as the following example

 <root><foo a="b">LOL</foo><bar value="ipsum" /></root>
4

Print the line on this way:

echo '<root><foo a="b">'"$lorem"'</foo><bar value="ipsum" /></root>' >> "$MY_XML"

This is need because single quotes deny shell interpreter from replace environment variables inside

3

Frustratingly perhaps, one of the combinations you omitted would have worked:

echo "<root><foo a=\"b\">$lorem</foo><bar value=\"ipsum\" /></root>" >> $MY_XML

Single quotes leave everything exactly as you type it. Escaping with \ will not work. Double quotes will evaluate the string. Inside double quotes you can use \ to escape specific characters (including the double-quote character itself).

You could also combine parts of the string that are quoted in different ways. Here, $lorem remains double-quoted but the rest of the string is surrounded with single quotes:

echo '<root><foo a="b">'"$lorem"'</foo><bar value="ipsum" /></root>' >> $MY_XML
2

Use in-line document with cat

cat >destfile <<EOF
<your ZML here...where $vas expand happly>
but the rest remain as is
EOF

I used it in a lot of scripts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.