Old thread, but this is a recurrent issue.
Here is a solution using bash's mapfile:
mateus@mateus:/tmp$ cat input.txt
a = $a
b = $b
mateus@mateus:/tmp$ echo a=$a, b=$b
a=1, b=2
mateus@mateus:/tmp$ function subst() { eval echo -E "$2"; }
mateus@mateus:/tmp$ mapfile -c 1 -C subst < input.txt
a = 1
b = 2
The bash builtin mapfile calls user-defined function subst (see -C/-c options) on each
line read from input file input.txt. Since the line contains unescaped text, eval is used tu evaluate it and echo the transformed text to stdout (-E avoid special characters to be interpreted).
IMHO this is a much more elegant than any sed/awk/perl/regex based solution.
Another possible solution is to use shell's own substitution. This looks like a more "portable" way not relaying on mapfile:
mateus@mateus:/tmp$ eval echo "\"$(cat <<EOF_$RANDOM
$(<input.txt)
EOF_$RANDOM
)\""
a = 1
b = 2
Note we use EOF_$RANDOM to minimize conflicting cat's here-document with input.txt content.
Both examples from: https://gist.github.com/4288846
EDIT: Sorry, the first example do not treats comments and white space correctly. I'll work on that and post a solution.