I have a set of variables populated eg
var1="test"
var2="test2"
I have a .env file as follows
var1
var2
var3=test3
I need to read the .env file and append the value of each variable to the relevant line unless the line already has a value. So what I would end up with in my .env is
var1=test
var2=test2
var3=test3
what I have so far is:
function populate_envs {
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ $line =~ "=" ]]; then
printf '%s\n' "$line"
else
printf '%s=$${%s}\n' "$line" "$line"
fi
done <$1
}
I'm currently printing the values to console so as not to change my file at the moment.
the problem is $${%s} is not working neither is $%s. I don't know how to get it to transpose the actual variable value.