How do I pass variables from the script to the files I am generating using cat?

Viewed 29

I am not sure if I am asking the question correctly.

This is what I working with:

a=foo
b=bar
cat 1.txt > new1.txt

Contents of 1.txt:

$a
$b

When run this, my new1.txt looks like this:

$a
$b

What I want:

foo
bar
2 Answers

With envsubst:

export a="foo"
export b="bar"
envsubst < 1.txt > new1.txt

Output to new1.txt:

foo
bar

With sed

sed "s/\$a/$a/; s/\$b/$b/" 1.txt > new1.txt
Related