How do I replace placeholders in a file with variables from another file? (Like docker-compose is doing it.)
I found many articles about single variable replacements or replacement by environment variables but not file to file.
.env file
name=John
time='10:00'
Content file
Hello "${name}"! I wait for u since ${time}.
Output file:
Hello "John"! I wait for u since 10:00.
Edit: Note that I also want to keep the "
in the file.
Edit2: I ended up using the solution from @steeldriver. This is what I use now in my script:
# Make copy of the template folder (containing scripts and `.env` file)
cp -r templates .templates
# Replace environment variables in all files of the folder
set -a
for file in $(find .templates -type f)
do
. ./.env && envsubst < $file > $file.tmp && mv $file.tmp $file
done
set +a
# create output directory
mkdir -p $HOME/output/
# copy if new or modified
rsync -raz .templates/. $HOME/output/
# remove temp folder
rm -r .templates