toren
toren

Reputation: 3

Shell Script Writing Variables Referenced In A File To File

Running into an issue that is likely simple for people that know how. I am writing an .sh script that reads in a file that has variable names in it. I am trying to use the file following this with the values of the variables in place for the names in the file.

My Attempted .sh Script:

HELLOVAR = "hello"
cat infilename.json > outfilename.json

I then attempt to use outfilename.json, but it has not changed from the original infile.

File In Example:

I would like this to be ${HELLOVAR}.

File Out Wanted:

I would like this to be hello.

Upvotes: 0

Views: 60

Answers (1)

William Pursell
William Pursell

Reputation: 212248

You probably just want:

HELLOVAR=hello envsubst < infilename.json > outfilename.json

envsubst reads its input stream looking for simple variable expressions (eg, it will substitute $FOO and ${FOO}, but will not perform the expected substitution on expressions like ${FOO-bar}) and expands them according to the current setting in the environment. HELLOVAR=hello envsubst invokes envsubst with HELLOVAR set to hello in its environment.

Upvotes: 1

Related Questions