Joseph Louthan
Joseph Louthan

Reputation: 181

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

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

Upvotes: 2

Views: 34

Answers (2)

Ivan
Ivan

Reputation: 7267

With sed

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

Upvotes: 0

Cyrus
Cyrus

Reputation: 88583

With envsubst:

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

Output to new1.txt:

foo
bar

Upvotes: 3

Related Questions