How to replace ${} placeholders in a text file?

Dana the Sane picture Dana the Sane · Jan 6, 2009 · Viewed 158.7k times · Source

I want to pipe the output of a "template" file into MySQL, the file having variables like ${dbName} interspersed. What is the command line utility to replace these instances and dump the output to standard output?

Answer

user picture user · Jan 6, 2009

Sed!

Given template.txt:

The number is ${i}
The word is ${word}

we just have to say:

sed -e "s/\${i}/1/" -e "s/\${word}/dog/" template.txt

Thanks to Jonathan Leffler for the tip to pass multiple -e arguments to the same sed invocation.