Shell Script Writing Variables Referenced In A File To File

Shell Script Writing Variables Referenced In A File To File

Problem Description:

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.

Solution – 1

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.

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.