How to substitute shell variables in complex text files

view story

http://stackoverflow.com – I have several text files in which I have introduced shell variables ($VAR1 or $VAR2 for instance). I would like to take those files (one by one) and save them in new files where all variables would have been replaced. To do this, I used the following shell script (found on StackOverflow): while read line do eval echo "$line" >> destination.txt done < "source.txt" This works very well on very basic files. But on more complex files, the "eval" command does too much: Lines starting with "#" are skipped XML files parsing results in tons of errors Is there a better way to do (HowTos)